[MongoDB] Connecting to Another DB from mongo shell to Execute Queries
I’ll introduce how to connect to another database and execute queries in MongoDB.
Let’s assume we’re managing collections separately across two DB servers.
Database 1
Database 2
When you want to handle collections from another DB while executing queries in mongo shell, you can create a connection to that DB.
$ mongo mongodb1/myDb
mongodb1> db2 = connect("mongodb2:27017/myDb")
connecting to: mongodb2:27017/myDb
myDb
// You can now handle data from somecollections saved in myDb on mongodb2 server
mongodb1> db2.somecollections.findOne()
When there was only one DB server, I didn’t need to worry about this, but with multiple servers, knowing tips for handling connections to different DB servers lets you keep executing queries from mongo shell as usual.
That’s all from the Gemba.