March 14 2016



On this page


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
username string Specifies an existing username with access privileges for this database.
password string Specifies the corresponding password.
mechanism string

Optional. Specifies the authentication mechanism used. Defaults to either:

Changed in version 3.0: In previous version, defaulted to MONGODB-CR.

For available mechanisms, see authentication mechanisms.

digestPassword boolean Optional. Determines whether the server receives digested or undigested password. Set to false to specify undigested password. For use withSASL/LDAP authentication since the server must forward an undigested password to saslauthd.

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


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.

Copyright 2019. All rights reserved.

Posted 2016年3月14日 by ucjmh in category "mongodb

Leave a Reply

Your email address will not be published. Required fields are marked *