In 2.x we're used to connecting to a connection string which specifies the database name:
MongoClient.connect('mongodb://localhost:27017/myDatabase', (err, db) => {
// Database returned (it's "myDatabase" as specified in the connection string)
});
In 3.0 there is now an extra step: calling client.db() with the database name, explicitly.
MongoClient.connect('mongodb://localhost:27017/myDatabase', (err, client) => {
// Client returned
var db = client.db('myDatabase'); // We have to repeat the database name here
});
How can we continue to use the database name that's already in the connection string? It's useful to keep the connection configuration to just 1 string, and not having to parse it out manually.
Right now I imagine a solution where I parse the database name out of the connection string which I pass to connect(), and then pass that to client.db(). But if MongoClient is already parsing the connection string internally, this seems like redundant work. Is there a simpler way?