- Reference >
mongoShell Methods >- User Management Methods >
- db.auth()
db.auth()¶
On this page
Definition¶
-
db.auth()¶ Allows a user to authenticate to the database from within the shell.
The
db.auth()method can accept either:the username and password.
db.auth( <username>, <password> )
a user document that contains the username and password, and optionally, the authentication mechanism and a digest password flag.
db.auth( { user: <username>, pwd: <password>, mechanism: <authentication mechanism>, digestPassword: <boolean> } )
Parameter Type Description usernamestring Specifies an existing username with access privileges for this database. passwordstring Specifies the corresponding password. mechanismstring Optional. Specifies the authentication mechanism used. Defaults to either:
SCRAM-SHA-1on new 3.0 installations and on 3.0 databases that have been upgraded from 2.6 with authSchemaUpgrade; orMONGODB-CRotherwise.
Changed in version 3.0: In previous version, defaulted to
MONGODB-CR.For available mechanisms, see authentication mechanisms.
digestPasswordboolean Optional. Determines whether the server receives digested or undigested password. Set to false to specify undigested password. For use with SASL/LDAP authentication since the server must forward an undigested password to saslauthd.Alternatively, you can use
mongo --username,--password, and--authenticationMechanismto specify authentication credentials.Note
The
mongoshell excludes alldb.auth()operations from the saved history.Returns: db.auth()returns0when authentication is not successful, and1when the operation is successful.