Why is there separate mongo.Server and mongo.Db in mongodb-native driver?

Viewed 2107

I am just learning mongodb-native driver for nodejs.

I connect like this.

var mongo=require("mongodb")

var serv=mongo.Server("localhost", 27017)
var dbase=mongo.Db("MyDatabase", serv)

And that works. But if I try to create a new database connection using the same server I get an error.

var dbase2=mongo.Db("MyDatabase2", serv)

"Error: A Server or ReplSet instance cannot be shared across multiple Db instances"

But it works if a make a new server connection first.

var serv2=mongo.Server("localhost", 27017)
var dbase2=mongo.Db("MyDatabase2", serv2)

So my question is why there are 2 connection functions, one for Server and one for Db, when it seems like they must always be used together?

Why doesn't it go like this.

var dbase=mongo.Db("localhost", 27017, "MyDatabase")

I want to make my own function that does this, but I wonder if there is some other reason they are separate.

Thanks.

3 Answers
Related