w3resource

MongoDB Authentication Commands

Introduction

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.

Syntax:

{ logout: 1 }

Example

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.

Syntax:

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.

Example

The following command will change the db object.

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

Previous: User Management Commands
Next: MongoDB INSERT



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mongodb/mongodb-authentication-commands.php