How to use MongoDB create user

The MongoDB Create User function allows you to add users to a database and assign roles to them. The method also works for people with administration rights. You can use the MongoDB Drop User command if you want to remove someone again.

What is MongoDB Create User?

The NoSQL database MongoDB offers several advantages for storing and managing your data. The Database Management System is also an excellent solution for those collaborating in small or large teams. However, it is important to keep track of who has access to your data, making it a good practice to create new users yourself and grant them certain rights. The Create User MongoDB Command enables you to do this. You can add a new user with this method and directly assign one or more roles to this person. You can also set a password for them.

What is the MongoDB Create User syntax?

The syntax of the MongoDB Create User command is as follows:

db.createUser (user, writeConcern)

Two other pieces of information are stored in this command in addition to the actual createUser command. “user” is a mandatory document and contains the authentication of the new user, along with all other necessary information. “writeConcern” indicates the authorization level of the person, which is optional.

The user and their parameters

You always need to include the “user” document when using the MongoDB Create User method. It contains several important parameters:

  • user: This string contains the name of the new user and is optional.
  • pwd: You can choose a password for the new person with Create User. This information is always required, unless you use the command on an external database.
  • customData: This document is optional and contains all desired additional information about the user, if necessary. For example, you can enter the user’s real name in addition to the username.
  • roles: You can assign built-in and user-defined roles to a user. The information is created in an array. The new user will not be assigned any roles if the field is left blank.

The “user” document looks like this:

{
user: "<username>",</username>
pwd: "<password>",</password>
customData: { <realname, id=""> },</realname,>
roles: [ { role: "<role>", db: "<database> " } ]</database></role>
}

You will get an error message if you attempt to add an already existing user with MongoDB Create User. This prevents overlaps occurring. Use the Show Users command or db.getUsers () to display all users in a database.

How do I assign roles with MongoDB Create User?

It is good practice to assign different roles to users right from the start. This enables you to work more effectively in a team and it protects sensitive data from unwanted access. You need to store the name of the role to define a role, provided it is listed in the same database. Otherwise, you need to create a document to define a role that exists in another database. Use the MongoDB Create User command to assign multiple roles:

use example
db.createUser(
{
}, "username",
pwd, "password",
roles: [ "read", "dbAdmin" ]
}
);

We have assigned the “read” and “dbAdmin” roles to the new user in this example.

How do I use MongoDB Create User without defining roles?

It is also possible to not assign roles to a user. However, you must clearly store this information in Create User. This works as follows:

use example
db.createUser(
{
}, "username",
pwd, "password",
roles: [ ]
}
);

The user will not receive any roles the brackets are left empty.

How do I add Admins on MongoDB Create User?

You can also create users with administrative tasks with the MongoDB Create User function. This person will then have access to your database’s configuration. The process is similar to code we have looked at already:

use admin
db.createUser(
{
user: "second_admin",
pwd: "admin_password",
roles: [ { role: "readWrite", db: "config" },
"clusterAdmin"
]
}
);

How do I delete a user with MongoDB Drop User?

You also have the option to delete a user at any time. The appropriate command for this is Drop User and it is similar in structure to Create User. Here is the syntax:

db.dropUser (username, writeConcern)

You only have to enter the name of the user:

use example
db.dropUser ("username")

Make sure that there is at least one other person who has the necessary privileges before deleting a user with administrative rights.

Tip

Would you like to learn more about the Database Management System in MongoDB? We recommend our other Digital Guide articles to get you up to speed:
- MongoDB Create Collection
- MongoDB Create Database
- MongoDB Create Index

Page top