Reputation: 13147
When I connect to MongoDB from the command-line using the mongo
command, I need to issue a use <db>
command to switch to the appropriate database before I can run a query, like this:
[mongo@mongotest ~] $ mongo localhost:10001
MongoDB shell version: 2.4.6
connecting to: localhost:10001/test
> use mydb01
switched to db mydb01
Now I can run my query:
> db.records.find({"contact.name": "Jack"});
Is there a way to consolidate those commands into a single command? For example:
> mydb01.records.find({"contact.name": "Jack"});
The MongoDB shell documentation makes it seem like there should be a way, but I haven't been able to find it.
Upvotes: 2
Views: 1183
Reputation: 69703
You can get a database object of another database than the one your are use
ing right now with db.getSiblingDB
.
> db.getSiblingDB("mydb01").records.find({"contact.name": "Jack"});
You can also store that object in a variable. That way you can easily work with many databases simultaneously.
> var mydb01 = db.getSiblingDB("mydb01");
> mydb01.records.find({"contact.name": "Jack"});
Upvotes: 2