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
Create a user
use admin

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

# Create a database user with roles.

Show users
db.getUsers()

# List users in the current database.

Grant additional roles
db.grantRolesToUser("appuser", [{ role: "dbAdmin", db: "appdb" }])

# Add roles to an existing user.

Change a user password
db.changeUserPassword("appuser", "new-strong-password")

# Rotate a database credential.

Drop a user
db.dropUser("appuser")

# Remove a user account.

## Admin and status
Check server status
db.adminCommand({ serverStatus: 1 })

# Inspect a detailed server-status document.

Inspect current operations
db.currentOp()

# See active operations on the server.

Kill an operation
db.killOp(12345)

# Stop a running operation by op id.

Ping the server
db.adminCommand({ ping: 1 })

# Verify the server is responsive.

Show version and build info
db.adminCommand({ buildInfo: 1 })

# Return server version metadata.

Recommended next

No recommendations yet.