runCommand() (Execute Command)
Last updated: 2026-10-09
runCommand() (Execute Command)
db.runCommand() executes any database command in mongosh — the generic entry point for low-level commands without dedicated helpers (serverStatus, collStats, currentOp). Most commands have helper methods (e.g. db.collection.stats()), but runCommand gives full control.
| Category | Admin Commands |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
db.runCommand({ <command>: <args>, ... })
⚙️ Parameters
| command name | First field of the command document; its value is the argument object |
|---|
Returns:The command result document with ok (1 success, 0 failure) plus command-specific fields
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// runCommand: database stats
db.runCommand({ dbStats: 1 })
// collection stats
db.runCommand({ collStats: "orders" })
Output:
{
db: "shop",
collections: 5,
objects: 1200,
dataSize: 48213,
ok: 1
}
📌 Notes
- First field is the command name, its value is the argument
- ok: 1 means success; ok: 0 failure (errmsg has the reason)
- Most commands have helpers — prefer them for readability
- Insufficient privileges yield unauthorized errors
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QHow do runCommand and helper methods relate?
AHelpers like db.stats() internally call runCommand, wrapping args and result parsing. runCommand is the underlying generic entry.
QHow do I see a command's help output?
AAdd $help: 1 to the arguments, e.g. db.runCommand({ serverStatus: 1, $help: 1 }).
QWhy do some commands require adminCommand?
AAdmin-scoped commands (renameCollection, setParameter, ...) live on the admin db and require db.adminCommand.