OPTIONS

db.auth()

Definition

db.auth(username, password)

Allows a user to authenticate to the database from within the shell.

Parameter Description
username Specifies an existing username with access privileges for this database.
password Specifies the corresponding password.
mechanism

Optional. Specifies the authentication mechanism used.

Changed in version 3.0.

Defaults to SCRAM-SHA-1 on new 3.0 installations and on 2.6 databases that have been upgraded to 3.0 and have undergone authSchemaUpgrade. Defaults to MONGODB-CR otherwise.

PLAIN is used for SASL/LDAP authentication, available only in MongoDB Enterprise.

Alternatively, you can use mongo --username, --password, and --authenticationMechanism to specify authentication credentials.

--authenticationMechanism supports additional mechanisms not available when using db.auth().

Note

The mongo shell excludes all db.auth() operations from the saved history.

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