w3resource

MongoDB: db.createUser() method

db.createUser()

The db.createUser() method is used to creates a new user.

Syntax:

db.createUser(user, writeConcern)

Creates a new user for the database where the method runs. db.createUser() returns a duplicate user error if the user already exists on the database.

The db.createUser() method has the following syntax:

Parameters:

Name Description Required /
Optional
Type
user The document with authentication and access information about the user to create. Required document
writeConcern The level of write concern for the creation operation. The writeConcern document takes the same fields as the getLastError command. Optional document

The user document defines the user and has the following form:

	{ user: "<name>",
  pwd: "<cleartext password>",
  customData: { <any information> },
  roles: [
    { role: "<role>", db: "<database>" } | "<role>",
    ...
  ]
}

The user document has the following fields:

Name Description Required /
Optional
Type
user The name of the new user. Optional string
pwd The user’s password. The pwd field is not required if you run db.createUser() on the $external database to create users who have credentials stored externally to MongoDB. Required string
customData 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. Optional document
roles The roles granted to the user. Can specify an empty array [] to create users without roles. Required array

In the roles field, you can specify both built-in roles and user-defined role.

To specify a role that exists in the same database where db.createUser() runs, 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.

The db.createUser() method wraps the createUser command.

Returns: db.auth() returns 0 when authentication is not successful, and 1 when the operation is successful.

Example: Create User with Roles

The following operation creates mynewuser in the test database and gives the user the readWrite and dbAdmin roles.

use test
db.createUser(
   {
     user: "mynewuser",
     pwd: "myuser123",
     roles: [ "readWrite", "dbAdmin" ]
   }
);

Output:

> use test
switched to db test
> db.createUser(
...    {
...      user: "mynewuser",
...      pwd: "myuser123",
...      roles: [ "readWrite", "dbAdmin" ]
...    }
... );
Successfully added user: { "user" : "mynewuser", "roles" : [ "readWrite", "dbAdmin" ] }

Example: Create User Without Roles

The following operation creates a user named mynewuser1 in the test database but does not yet assign roles:

use test
db.createUser(
   {
     user: "mynewuser1",
     pwd: "myuser1123",
     roles: [ ]
   }
);

Output:

> use test
switched to db test
> db.createUser(
...    {
...      user: "mynewuser1",
...      pwd: "myuser1123",
...      roles: [ ]
...    }
... );
Successfully added user: { "user" : "mynewuser1", "roles" : [ ] }

Example: Create Administrative User with Roles

The following operation creates a user named myadmin1 in the admin database and gives the user readWrite access to the config database, which lets the user change certain settings for sharded clusters, such as to the balancer setting.

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

Output:

> use admin
switched to db admin
> db.createUser(
...    {
...      user: "myadmin1",
...      pwd: "myadmin123",
...      roles:
...        [
...          { role: "readWrite", db: "config" },
...          "clusterAdmin"
...        ]
...    }
... );
Successfully added user: {
        "user" : "myadmin1",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "config"
                },
                "clusterAdmin"
        ]
}

Retrieve the restaurants data from here

Behavior

Encryption

db.createUser() sends password to the MongoDB instance without encryption. To encrypt the password during transmission, 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 createUser action on that database resource.
  • To grant roles to a user, you must have the grantRole action on the role’s database.

Built-in roles userAdmin and userAdminAnyDatabase provide createUser and grantRole actions on their respective resources.

Previous: db.auth() method
Next: db.updateUser() method



Follow us on Facebook and Twitter for latest update.