AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

MongoDB Create User: add a new user

PARTAGEZ

The MongoDB Create User function allows you to add new users to a database and assign them roles. This method also works for people with admin rights. If you want to remove someone, use the MongoDB Drop User command.

What is MongoDB Create User?

The NoSQL MongoDB database is a very good solution for storing and managing your own data. The database management system is also suitable for collaboration within small or large teams. However, so that you can always keep an overview of who has access to your data, create new users yourself and then assign them certain rights. The appropriate command among the MongoDB commands is Create User. This method allows you not only to add a new user, but also to directly assign one or more roles to this person. It is also possible to define a password for it.

MongoDB Create User Syntax

The MongoDB Create User command syntax looks like this:

db.createUser (user, writeConcern)

In addition to the actual createUser command, two other pieces of information are already entered here: “user” is a mandatory document containing authentication of the new user, as well as all other data. “writeConcern” indicates the authorization level of the person. However, this information is optional.

The user and his settings

The “user” document, which you must always insert into MongoDB for the “Create User” method, contains several important parameters:

  • to use : This string contains the name of the new user and is optional.
  • pwd : under “pwd” you set a password for the new person in Create User. This information is always required unless you are using the command against an external database.
  • customData : this document is optional and contains, if necessary, all the additional information desired on a user. For example, you must indicate the real name of the user in addition to the pseudonym used.
  • roles : Here you assign a user to built-in and user-defined roles. The information is entered into an Array. The new user will not get any roles if you leave the field blank.

The “user” document looks like this, for example:

{
user: "<nom d’utilisateur="">",</nom>
pwd: "<mot de="" passe="">",</mot>
customData: { <vrai nom,="" id=""> },</vrai>
roles: [ { role: "<rôle>", db: "<base de="" données="">" } ]</rôle>
}

If you are working with MongoDB and Create User and using an already existing user, you will receive an error message. This helps to avoid overlaps. To show all users in a database, use in MongoDB either Show Users or db.getUsers().

Assign roles with MongoDB Create User

In principle, it is recommended to assign different roles to users from the start. This allows you to work more effectively as a team while protecting sensitive data against unwanted access. If you want to define roles, you can do so by registering the role name, provided it is in the same database. Otherwise, create a document to define the role that already exists in another database. Here’s how to give multiple roles to one person with the MongoDB Create User command:

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

In this example, we have now assigned the “read” and “dbAdmin” roles to the new user.

MongoDB Create User without assigning roles

If you do not want to assign a role to a user, this is also possible. However, you must clearly indicate this information for Create User. Here’s how it works:

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

If you leave the parentheses empty, the user won’t get any roles.

MongoDB Create User for Admins

The MongoDB Create User function also allows you to create users responsible for administrative tasks. This person will then also have access to your database configuration. The procedure is similar to the already known code:

use admin
db.createUser(
{
user: "deuxième_administrateur",
pwd: "mot_de_passe_administrateur",
roles: [ { role: "readWrite", db: "config" },
"clusterAdmin"
]
}
);

Delete a user with MongoDB Drop User

You also have the option of deleting a user at any time. The proper command for this is Drop User and resembles Create User in structure. Here is its syntax:

db.dropUser (username, writeConcern)

Here you only need to specify the username:

use exemple
db.dropUser ("username")

Before deleting a user with administrative privileges, you must first ensure that at least one other person has the necessary permissions.

Advice

Want to learn more about using the MongoDB database management system? Then we recommend reading our other IONOS Digital Guide articles on this topic:
– MongoDB Create Collection
– MongoDB Create database
– MongoDB Create Index

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact