IntroductionThe 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. 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 CommandsCertain 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"); // shut down the database If the db variable is not set to 'admin', you can use adminCommand (_adminCommand in versions earlier than 1.8) 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 CommandUse 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 |

PLEASE POST QUESTIONS IN THE FORUMS: http://groups.google.com/group/mongodb-user. Post tips and clarifications here.
blog comments powered by Disqus