Database Reference
In-Depth Information
The results are identical to what's provided by the stats() method. Note that the
command is defined by the document {dbstats: 1} . In general, you can run any
available command by passing its document definition to the runCommand method.
Here's how you'd run the collection stats command:
> db.runCommand( {collstats: 'numbers'} )
The output should look familiar.
But to get to the heart of database commands, you need to see how the run-
Command() method actually works. That's not hard to find out because the MongoDB
shell will print the implementation of any method whose executing parentheses are
omitted. So instead of running the command like this
> db.runCommand()
You can execute the parentheses-less version and see the internals:
> db.runCommand
function (obj) {
if (typeof obj == "string") {
var n = {};
n[obj] = 1;
obj = n;
}
return this.getCollection("$cmd").findOne(obj);
}
The last line in the function is nothing more than a query on the $cmd collection. To
define it properly, then, a database command is a query on a special collection, $cmd ,
where the query selector defines the command itself. That's all there is to it. Can you
think of a way to run the collection stats command manually? It's this simple:
db.$cmd.findOne( {collstats: 'numbers'} );
Using the runCommand helper is easier, but it's always good to know what's going on
just beneath the surface.
2.4
Getting help
By now, the value of the MongoDB shell as a testing ground for experimenting with
data and administering the database should be evident. But since you'll likely be
spending a lot of time in the shell, it's worth knowing how to get help.
The built-in help commands are the first place to look. db.help() prints a list of
commonly used methods for operating on database objects. You'll find a similar list of
methods for operating on collections by running db.foo.help() .
There's also built-in tab completion. Start typing the first characters of any method
and then press the Tab key twice. You'll see a list of all matching methods. Here's the
tab completion for collection methods beginning with get :
> db.foo.get
db.foo.getCollection(
db.foo.getIndexSpecs(
db.foo.getName(
Search WWH ::




Custom Search