Databases Reference
In-Depth Information
customers
> game = db.getSisterDB("game")
game
Now you can use them in the same way you'd use db : game.players.find() , custom
ers.europe.update() , etc.
You can also connect db , or any other variable, to another server:
> db = connect("ny1a:27017/foo")
connecting to: ny1a:27017/foo
foo
> db
foo
This can be particularly handy if you are running a replica set or sharded cluster and
you want to connect to more than one node. You could maintain separate connections
to the master and slave in your shell:
> master = connect("ny1a:27017/admin")
connecting to: ny1a:27017/admin
admin
> slave = connect("ny1b:27017/admin")
connecting to: ny1b:27017/admin
admin
You can also connect directly to shards, config servers, or any MongoDB server you
have running.
Some shell functions, notably the rs helpers, assume you're using db as
your database. If db is connected to a slave or arbiter, some helpers won't
work.
One annoyance with connecting to multiple servers through the shell is that MongoDB
keeps track of all of the connections you've ever made and, if one goes down, will
complain incessantly about it until you bring the server back up or restart the shell.
Even undefining the connection doesn't reset this! This will be fixed in version 1.9, but
it is currently harmless but noisy.
Tip #47: Get “help” for any function
JavaScript lets you see the source code for most of the functions you run in the shell.
If you're curious as to what arguments a function takes or can't remember what it
returns, you can see the source by running the function name, without parentheses.
For example, suppose we remember that db.addUser adds a user, but we can't remember
exactly what the arguments are:
> db.addUser
function (username, pass, readOnly) {
readOnly = readOnly || false;
var c = this.getCollection("system.users");
 
Search WWH ::




Custom Search