2

In a project I am using node-mongodb-native to connect to a database with some collections.

Now there's another database containing one other collection needed. I want to query this collection from the same project, with the minimum amount of code change.

I know MongoEngine does this rather pretty.

Is there a standard way of doing this in NodeJS, e.g. with the node-mongodb-native driver?

1 Answer 1

1

I suppose this is the way you are connecting to the mongo DB, Similarly you can create a new Mongo client variable with a different name to connect to the new db.

var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  insertDocuments(db, function() {
    db.close();
  });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.