logout (Sign Out)
Last updated: 2026-10-09
logout (Sign Out)
logout ends the authentication session of the current connection, signing the user out; in mongosh call db.logout(). Typical uses are scripts and shared connections switching identities. After logout, operations on the connection raise unauthorized errors until re-authentication. It takes no arguments and matters only when authorization is on.
| Category | User Authentication |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
db.logout()
Returns:Returns { ok: 1 } on success
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// logout: end the session's authentication
db.logout()
// further operations now require auth
db.users.findOne()
Output:
{ ok: 1 }
// findOne raises a NotAuthorized error
📌 Notes
- Affects only the current connection/session
- Re-authenticate before further operations
- Pointless when authorization is disabled
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QDoes logout affect all connections?
ANo — only the current connection's authentication state.
QCan I keep operating after logout?
AOnly after re-authenticating with db.auth().
QWhen should I call logout?
AWhen switching identities on shared connections or closing a script's session cleanly.