- Reference >
- Database Commands >
- User Management Commands >
- createUser
createUser¶
On this page
Definition¶
-
createUser¶ Creates a new user on the database where you run the command. The
createUsercommand returns a duplicate user error if the user exists. ThecreateUsercommand uses the following syntax:{ createUser: "<name>", pwd: "<cleartext password>", customData: { <any information> }, roles: [ { role: "<role>", db: "<database>" } | "<role>", ... ], writeConcern: { <write concern> } }
createUserhas the following fields:Field Type Description createUserstring The name of the new user. pwdstring The user’s password. The pwdfield is not required if you runcreateUseron the$externaldatabase to create users who have credentials stored externally to MongoDB.customDatadocument Optional. Any arbitrary information. This field can be used to store any data an admin wishes to associate with this particular user. For example, this could be the user’s full name or employee id. rolesarray The roles granted to the user. Can specify an empty array []to create users without roles.digestPasswordboolean Optional. When true, themongodinstance will create the hash of the user password; otherwise, the client is responsible for creating the hash of the password. Defaults totrue.writeConcerndocument Optional. The level of write concern for the creation operation. The writeConcerndocument takes the same fields as thegetLastErrorcommand.In the
rolesfield, you can specify both built-in roles and user-defined role.To specify a role that exists in the same database where
createUserruns, you can either specify the role with the name of the role:"readWrite"Or you can specify the role with a document, as in:
{ role: "<role>", db: "<database>" }
To specify a role that exists in a different database, specify the role with a document.
Behavior¶
Encryption¶
createUser sends password to the MongoDB instance in
cleartext. To encrypt the password in transit, use TLS/SSL.
External Credentials¶
Users created on the $external database should have credentials
stored externally to MongoDB, as, for example, with MongoDB
Enterprise installations that use Kerberos.
local Database¶
You cannot create users on the local database.
Required Access¶
- To create a new user in a database, you must have the
createUseraction on that database resource. - To grant roles to a user, you must have the
grantRoleaction on the role’s database.
The userAdmin and
userAdminAnyDatabase built-in roles
provide createUser and
grantRole actions on their respective resources.
Example¶
The following createUser command creates a user accountAdmin01 on the
products database. The command gives accountAdmin01 the
clusterAdmin and readAnyDatabase roles on the admin database
and the readWrite role on the products database:
db.getSiblingDB("products").runCommand( { createUser: "accountAdmin01",
pwd: "cleartext password",
customData: { employeeId: 12345 },
roles: [
{ role: "clusterAdmin", db: "admin" },
{ role: "readAnyDatabase", db: "admin" },
"readWrite"
],
writeConcern: { w: "majority" , wtimeout: 5000 }
} )