Shell Help
There are a number of functions that give you a little help if you forget a command:
> // basic help
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
...
Note that there are separate help functions for databases, collections, replica sets, sharding, administration, and more. Although not listed explicitly, there is also help for cursors:
> // list common cursor functions
> db.foo.find().help()
You can use these functions and helpers as built-in cheat sheets.
Seeing Function Definitions
If you don’t understand what a function is doing, you can run it without the parentheses in the shell to see its source code:
> // run the function
> db.serverCmdLineOpts()
{ “argv” : [ “./mongod” ], “parsed” : { }, “ok” : 1 }
> // see its source
> db.serverCmdLineOpts
This can be helpful for seeing what arguments it expects or what errors it can throw, as well as how to run it from another language.
Using edit
The shell has limited multi-line support, so it can be difficult to program in. The shell helper edit makes this easier, opening up a text editor and allowing you to edit variables from there. For example:
<xmp>
> x = function() { /* some function we’re going to fill in */ }
> edit x
<opens emacs with the contents of x>
</xmp>
Modify the variable in your editor, then save and exit. The variable will be set in the shell.
Either the EDITOR environment variable or a MongoDB shell variable EDITOR must be set to use edit. You can set it in the MongoDB shell as follows:
<xmp$gt;
> EDITOR=”/user/bin/emacs”
</xmp>
edit is not available from JavaScript scripts, only in the interactive shell.
.mongorc.js
If a .mongorc.js file exists in your home directory, it will automatically be run on shell startup. Use it to initialize any helper functions you use regularly and remove functions you don’t want to accidentally use.
For example, if you would prefer to not have dropDatabase() available by default, you could add the following lines to your .mongorc.js file:
<xmp>
DB.prototype.dropDatabase = function() {
print(“No dropping DBs!”);
}
db.dropDatabase = DB.prototype.dropDatabase;
</xmp>
The example above will change the dropDatabase() helper to only print a message, and not to drop databases.
Note that this technique should not be used for security because a determined user can still drop a database without the helper. However, removing dangerous admin commands can help prevent fat-fingering.
A couple of suggestions for helpers you may want to remove from .mongorc.js are:
- DB.prototype.shutdownServer
- DBCollection.prototype.drop
- DBCollection.prototype.ensureIndex
- DBCollection.prototype.reIndex
- DBCollection.prototype.dropIndexes
Changing the Prompt
The shell prompt can be customized by setting the prompt variable to a function that returns a string:
<xmp>
prompt = function() {
try {
db.getLastError();
}
catch (e) {
print(e);
}
return (new Date())+”$ “;
}
</xmp>
If you set prompt, it will be executed each time the prompt is drawn (thus, the example above would give you the time the last operation completed).
Try to include the db.getLastError() function call in your prompt. This is included in the default prompt and takes care of server reconnection and returning errors from writes.
Also, always put any code that could throw an exception in a try/catch block. It’s annoying to have your prompt turn into an exception!
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}