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.

CategoryAdmin Commands
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

db.runCommand({ <command>: <args>, ... })

⚙️ Parameters

command nameFirst 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏