- Reference >
mongoShell Methods >- Database Methods >
- db.logout()
db.logout()¶
-
db.logout()¶ Ends the current authentication session. This function has no effect if the current session is not authenticated.
Note
If you’re not logged in and using authentication,
db.logout()has no effect.Because MongoDB allows users defined in one database to have privileges on another database, you must call
db.logout()while using the same database context that you authenticated to.If you authenticated to a database such as
usersor$external, you must issuedb.logout()against this database in order to successfully log out.Example
Use the
use <database-name>helper in the interactivemongoshell, or the followingdb.getSiblingDB()in the interactive shell or inmongoshell scripts to change thedbobject:When you have set the database context and
dbobject, you can use thedb.logout()to log out of database as in the following operation:db.logout()function provides a wrapper around the database commandlogout.