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 ad­min­is­tra­tion 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 ad­vant­ages for storing and managing your data. The Database Man­age­ment System is also an excellent solution for those col­lab­or­at­ing 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 in­form­a­tion are stored in this command in addition to the actual cre­ateUser command. “user” is a mandatory document and contains the au­then­tic­a­tion of the new user, along with all other necessary in­form­a­tion. “write­Con­cern” indicates the au­thor­iz­a­tion level of the person, which is optional.

The user and their para­met­ers

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

  • 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 in­form­a­tion is always required, unless you use the command on an external database.
  • cus­tom­Data: This document is optional and contains all desired ad­di­tion­al in­form­a­tion 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 in­form­a­tion 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 ef­fect­ively 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 in­form­a­tion 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 ad­min­is­trat­ive tasks with the MongoDB Create User function. This person will then have access to your database’s con­fig­ur­a­tion. 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 ap­pro­pri­ate 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 priv­ileges before deleting a user with ad­min­is­trat­ive rights.

Tip

Would you like to learn more about the Database Man­age­ment System in MongoDB? We recommend our other Digital Guide articles to get you up to speed:
- MongoDB Create Col­lec­tion
- MongoDB Create Database
- MongoDB Create Index

Go to Main Menu