1

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.

Running Turtle
  • 12,360
  • 20
  • 55
  • 73
  • possible duplicate of [Using Multiple Mongodb Databases with Meteor.js](http://stackoverflow.com/questions/20535755/using-multiple-mongodb-databases-with-meteor-js) – chridam Mar 11 '15 at 14:28

1 Answers1

3

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.

Jasper Woudenberg
  • 1,156
  • 9
  • 15
  • Well yes but it doesn't really demonstrate what the OP asked does it. Would be better to show how to connect so that information can be passed from db1 to db2 – Neil Lunn Mar 12 '15 at 04:09
  • The question is about opening multiple connections. Passing data from database to another database being an illustration of why the OP wants to do that. Focusing on doing a complete example of the latter would drown the comparatively simple answer to the former, so I'm assuming the OP has some knowledge of asynchronous programming in javascript. If the actual communication between dbs part still poses problems, I'd be happy to elaborate there. That said, I feel you have a point my example actually diverges from the end goal the OP has in mind, so I changed it to better that purpose. – Jasper Woudenberg Mar 12 '15 at 06:54
  • So why is it so hard for you to understand that you open one connection inside the callback to the other? – Neil Lunn Mar 12 '15 at 06:58
  • Maybe adding your own answer is a more constructive expenditure of your time than making personal attacks. – Jasper Woudenberg Mar 12 '15 at 08:05
  • What personal attack. I tried to prompt you to improve your answer. You responded with a long rant and an edit that while doing the right thing ultimately is very obtuse for a beginner to understand. I would suggest that you might consider that the attitude is yours and you possibly should have put half as much effort into your answer as you have in seeming to take offence from offered help. – Neil Lunn Mar 12 '15 at 08:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72817/discussion-between-jasper-woudenberg-and-neil-lunn). – Jasper Woudenberg Mar 12 '15 at 08:43