MongoDB Authentication Commands


In this page, we are going to discuss the commands which are used to operate the database. We discuss on this page that, how to terminates the current authenticated session for a database, how to start a session by an authenticated user.

logout Command

The logout command terminates the current authenticated session. If you’re not logged in and using authentication, logout has no effect. If you authenticated to a database such as users or $external, you must issue logout against this database in order to successfully log out.


{ logout: 1 }


The following command will change the db object.

> use userdetails
switched to db userdetails

When you have set the database context and db object, you can use the logout to log out of database as in the following operation:

db.runCommand( { logout: 1 } )

authenticate Command

The authenticate command is used to authenticate a connection. db.auth() returns 0 when authentication is not successful, and 1 when the operation is successful.


db.auth(username, password)
Parameter Type Description
username string Specifies an existing username with access privileges for this database.
password string Specifies the corresponding password.

Alternatively the command "mongo --username --password" can be used to specify authentication credentials.


The following command will change the db object.

> db.auth( "myNewuser", "thisPassword" );

Previous: User Management Commands
Next: MongoDB INSERT

Share this Tutorial / Exercise on : Facebook and Twitter