Mongo DB command in Depth

1.   The MongoDB command interface provides access to all non CRUD database operations.

 

 

2.   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:

db.$cmd.findOne( { <commandname>: <value> [, options] } );
 

 

The shell provides a helper function for this:

db.runCommand( { <commandname>: <value> [, options] } );
 

 

 

3.   All commands return, at minimum, a document with an ok field indicating whether the command has succeeded:

{ 'ok': 1 }
 

 

If the command fails, the value of ok will be 0.

 

4.  For many db commands, some drivers implement wrapper methods to make usage easier. To check our database's current profile level setting:

> db.runCommand({profile:-1});

{

    "was" : 0.0 ,

    "ok" : 1.0

}
 

Or, you can use a corresponding wrapper method:

 

> db.getProfilingLevel()

0.0 
 

Its implementation is:

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

} 
 

 

5.  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");
 

6.   Use commandHelp in shell to get help info for a command:

> db.commandHelp("datasize")
 

 

 

7.   For detailed command reference, please refer :

http://docs.mongodb.org/manual/reference/commands/

猜你喜欢

转载自seanzhou.iteye.com/blog/1573554