For those that want to continue using version ^3.0.1 be aware of the changes to how you use the MongoClient.connect()
method. The callback doesn't return db
instead it returns client
, against which there is a function called db(dbname)
that you must invoke to get the db
instance you are looking for.
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");
const db = client.db(dbName);
client.close();
});