Usually MongoClient is preferred, the only serious problem is that it is newer (1.2 +).
Here is a guide:
MongoClient or how to connect in a new and better way
From driver version 1.2 we introduce a new connection class that has the same name in all of our official drivers. This is necessary so that we present a recognizable front for all our APIs. This does not mean that your existing application will break, but rather that we ask you to use the new api connection to simplify application development.
In addition, we create a new connection class. MongoClient confirms all entries in MongoDB, in contrast to the existing Db connection class, which has confirmations disabled.
Thus, the two biggest changes are the fact that MongoClient confirms all entries in the database and when the actual database is selected in the connection.
With MongoClient:
var MongoClient = require('mongodb').MongoClient , Server = require('mongodb').Server; var mongoClient = new MongoClient(new Server('localhost', 27017)); mongoClient.open(function(err, mongoClient) { var db1 = mongoClient.db("mydb");
vs with Db:
// db is selected in the next line, unlike with MongoClient and most drivers to other databases var db = new Db('test', new Server('locahost', 27017)); // Establish connection to db db.open(function(err, db) { assert.equal(null, err); db.on('close', test.done.bind(test)); db.close(); });
source share