MongoDB User Management Commands

The mongo DB User management command contains the commands related to the users. We can create, delete, and update the users using the following User Management Commands.

MongoDB createUser command

The MongoDB createUser command creates a new user for the database from where we run the command. If the user already exists, it will return the duplicate user error.

Syntax:

The createUser command has the following field:

FieldTypeDescription
createUserstringThis field contains the name of the new user.
pwdstringThis field contains the user's password. The value can be either the user's password in cleartext string or passwordPrompt() to prompt for the user's password.
customDatadocumentThis field contains the data that an admin wishes to associate with the particular user.
rolesarrayThe field grants any role to the user.
digestPasswordbooleanThe digestPassword indicates that it is a server or a client who digests the password.
writeConcerndocumentThis field contains the write concern for the creation operation.
authentication
Restrictions
arrayIt enforces the authentication rules on the created user. It provides a list of IP addresses and CIDR ranges from which the user is allowed to connect.
mechanismarrayThis field specifies the SCRAM mechanisms. The valid SCRAM values are SCRAM-SHA-1 and SCRAM-SHA-256.

Example:

The above example creates a user admin@javaTpoint on the student database. The command gives admin@javatpoint the clusterAdmin and readAnyDatabase roles on the admin database and the readwrite role on the student's database.

MongoDB dropUser command

The MongoDB dropUser command deletes the user from the database on which we are running the command.

Syntax:

The dropUser command fields:

FieldTypeDescription
dropUserstringThe dropUser field contains the name of the user that you want to delete.
writeConcerndocumentThis field contains the write concern level for the removal operation.

Example:

MongoDB updateUser command

The MongoDB updateUser command updates the users details in the database on which we run the command. It will completely replace the previous field's values when we use the command, including the roles that are assigned and the authenticationRestrictions arrays.

Syntax:


FieldTypeDescription
updateUserstringIt contains the name of the user that we need to update.
pwdstringIt contains the user's password, or you can use the password prompt to prompt for the password.
customDatadocumentThis field contains the data that an admin wishes to update in the particular user.
rolesarrayThis field grants a role to the user.
digestPasswordbooleanIt indicates, if the server or client will digest the password.
writeConcerndocumentThis field contains the write concern for the creation operation.
authentication
Restrictions
arrayIt enforces the authentication rules on the created user. It provides a list of IP addresses and CIDR ranges from which the user is allowed to connect.
mechanismarrayThis field specifies the SCRAM mechanisms. The valid SCRAM values are SCRAM-SHA-1 and SCRAM-SHA-256.

Example:

The following update user command completely replaces the user's customData and roles data:






Latest Courses