db.auth(username,password) in mongodb

auth(username, password)

This method allows a user to authenticate the database from within the shell.

The db.auth() method can accept either the username and password as:

Case 1. username and password.

db.auth( <username>, <password> )

Case 2. 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>

})

Parameters Description:

username: The name of the user. It is required.

password:  The user password value. It is also required.

mechanismThe authentication mechanism to use. It is optional

digetPassword: Determines whether or not the supplied password should be pre-hashed before being used with the specified authentication mechanism. The default value is false.It is optional.


Example 1:  When no argument is passing to the method.

db.auth()

Output:

Error: auth expects either (username, password) or ({ user: username, pwd: password }) 0


Example 2: When username and password are passed as arguments but any one or both are wrong.

db.auth(username,password)


Output:

Error: Authentication failed.


Example 3: When username and password are passed as arguments and both are correct.

db.auth(username,password)


Output:

Info: Authentication success.


No comments:

Post a Comment