4

Is there a way to use multiple databases with a single connection to mongodb? I've found this:

https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html#open

but as best I can tell those docs are old as there does not appear to be an open method on the MongoClient? Do you actually need to establish multiple connections?

Thanks!

wakeda
  • 71
  • 1
  • 4

1 Answers1

3

Found it:

http://mongodb.github.io/node-mongodb-native/2.0/api/Db.html#db

Here is their example

var MongoClient = require('mongodb').MongoClient,
  test = require('assert');
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
  test.equal(null, err);

  // Reference a different database sharing the same connections
  // for the data transfer
  var secondDb = db.db("integration_tests_2");
  ...

It is synchronous. Seems strange to me this method doesn't have the word "use" in it. Also seems strange it belongs to the db class. db.db('other_db').. a bit obscure. Did some tests, seems to work, so I'll mark this as the answer for anyone else that ends up here.

wakeda
  • 71
  • 1
  • 4