Introduction
The Mongo database has a concept of a database command. Database commands are ways to ask the database to perform special operations, or to request information about its current operational status.
- [List of Database Commands]
A command is sent to the database as a query to a special collection namespace called $cmd. The database will return a single document with the command results - use findOne() for that if your driver has it.
The general command syntax is:
db.$cmd.findOne( { <commandname>: <value> [, options] } );
The shell provides a helper function for this:
db.runCommand( { <commandname>: <value> [, options] } );
For example, to check our database's current profile level setting, we can invoke:
> db.runCommand({profile:-1});
{
"was" : 0.0 ,
"ok" : 1.0
}
For many db commands, some drivers implement wrapper methods are implemented to make usage easier. For example, the [mongo shell ] offers
> db.getProfilingLevel()
0.0
Let's look at what this method is doing:
> print( db.getProfilingLevel )
function () {
var res = this._dbCommand({profile:-1});
return res ? res.was : null;
}
> print( db._dbCommand )
function (cmdObj) {
return this.$cmd.findOne(cmdObj);
}
Many commands have helper functions - see your driver's documentation for more information.
Privileged Commands
Certain operations are for the database administrator only. These privileged operations may only be performed on the special database named admin.
> use admin;
> db.runCommand("shutdown");
If the db variable is not set to 'admin', you can use _adminCommand to switch to the right database automatically (and just for that operation):
> db._adminCommand("shutdown");
(For this particular command there is also a shell helper function, db.shutdownServer.)
Getting Help Info for a Command
Use commandHelp in shell to get help info for a command:
> db.commandHelp("datasize")
help for: datasize example: { datasize:"blog.posts", keyPattern:{x:1}, min:{x:10}, max:{x:55} }
NOTE: This command may take awhile to run
(Help is not yet available for some commands.)
More Command Documentation
IF YOU HAVE A QUESTION, POST IT TO THE USER GROUP.
These pages are fine for comments, but for questions, your best bet will always be the MongoDB User Group. blog comments powered by Disqus