简体   繁体   中英

Is it possible to create connections to multiple remote MongoDB databases?

Using mongojs or any other nodejs library, is it possible to create connections to multiple remote MongoDB databases (not replicated dbs) ?

What I need to do is fetch some data in a collection in remote database A, process it then update some other documents in a collection in remote database B.

Sure. For instance, using the nodejs mongodb driver :

var MongoClient = require('mongodb').MongoClient
MongoClient.connect('<connectionstring1>', function (err, db1) {
    MongoClient.connect('<connectionstring2>', function (err, db2) {
        //Do something with db1 and db2 here.
    });
});

As you can see, you can connect to as many databases as you like in this fashion.

If you're not to fond of the nested callbacks, consider taking a look at the async library to clean that up a bit.

Let me know if this works to you!

Thanks to Neil Lunn for his feedback on the previous versions of this answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM