MongoDB Users, Roles, and Administrative Commands

Create users, grant roles, inspect server status, and run the most common administrative commands in MongoDB.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Users and roles

Basic authentication and role-management workflows.

Create a user

Create a database user with roles.

javascriptANYmongodbcreateUserroles
javascript
use admin

db.createUser({
  user: "appuser",
  pwd: "strong-password",
  roles: [{ role: "readWrite", db: "appdb" }]
})

Create users in the appropriate database, often `admin` for centralized management.

Show users

List users in the current database.

javascriptANYmongodbuserssecurity
javascript
db.getUsers()

Useful when auditing local database users.

Grant additional roles

Add roles to an existing user.

javascriptANYmongodbgrantRolesToUserroles
javascript
db.grantRolesToUser("appuser", [{ role: "dbAdmin", db: "appdb" }])

Roles can be layered as responsibilities change.

Change a user password

Rotate a database credential.

javascriptANYmongodbpasswordsecurity
javascript
db.changeUserPassword("appuser", "new-strong-password")

Routine credential rotation is easier when you know the exact shell command.

Drop a user

Remove a user account.

javascriptANYmongodbdropUsersecurity
javascript
db.dropUser("appuser")

Useful during environment cleanup or credential revocation.

Admin and status

Operational commands for quick diagnostics.

Check server status

Inspect a detailed server-status document.

javascriptANYmongodbserverStatusadmin
javascript
db.adminCommand({ serverStatus: 1 })

Returns deep operational detail; it is often used for diagnostics, monitoring, and troubleshooting.

Inspect current operations

See active operations on the server.

javascriptANYmongodbcurrentOpoperations
javascript
db.currentOp()

Helpful when tracking down long-running operations or lock contention.

Kill an operation

Stop a running operation by op id.

javascriptANYmongodbkillOpoperations
javascript
db.killOp(12345)

Use carefully after confirming the operation is safe to terminate.

Ping the server

Verify the server is responsive.

javascriptANYmongodbpingadmin
javascript
db.adminCommand({ ping: 1 })

A lightweight connectivity check.

Show version and build info

Return server version metadata.

javascriptANYmongodbbuildInfoversion
javascript
db.adminCommand({ buildInfo: 1 })

Useful when you need to confirm the running server version and build details.

Recommended next

No recommendations yet.