Commands

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.

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"); // 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 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

getLog Command
Index-Related Commands
flushRouterConfig command
Viewing and Terminating Current Operation
fsync Command
Replica Set Commands
removeshard command
cloneCollection Command
List of Database Commands
Commands
setParameter Command
getLastError Command
Validate Command
Moving Chunks
Compact Command
Showing first 15 of 17 results

Labels

commands commands Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

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

blog comments powered by Disqus