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

3

10

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.

Crotchety answered 15/8, 2012 at 14:0 Comment(0)
S
3

Here is a link to the solution on the mongo docs, for reference. (seems like the same solution the other poster mentioned)

http://mongodb.github.com/node-mongodb-native/markdown-docs/database.html#sharing-the-connections-over-multiple-dbs

The point of separating the connection to the mongo server, and then the DB is for cases like when you want to connect to a ReplSet server, or other custom params. This way, you have a separate process connecting to a mongodb server.

The database connection call is separate simply because of the case you have here: you dont simply want to connect to a mongo server and a single db, but multiple dbs. This separation of connecting to db and server allows this flexibility.

Another Solution: Use node-mongoskin

Mongoskin does what you want to... it allows connecting to server and db all in one command. Not a solution for mongo-native, but worth considering as an alternative library for your future projects.

var mongo = require('mongoskin');
var db = mongo.db('localhost:27017/testDB');
Spermatium answered 29/8, 2012 at 15:38 Comment(3)
The separate database connections could be done even if they didn't have a separate Server constructor. The multiple connections are not done using the Db constructor, but rather the .db property on the instance of the Db constructor. But could you explain what you mean about the ReplSet server? Is it something that is useful without calling the Db constructor?Crotchety
Well, mongo lets you have separate mongodb servers running that replicate data, and you would connect to them all together example: ('192.168.0.1:27017/?auto_reconnect=true','192.168.0.2:27017/?auto_reconnect=true','192.168.0.3:27017/?auto_reconnect=true')Spermatium
updated answer with more info. mentioned mongo-skin which is a wrapper for mongo-native and connects to db and server in one commandSpermatium
B
3

For what it's worth, you can do what you want to do by using Db#db(), which doesn't seem to appear in the official documentation but is listed in the source code of db.js as being a public API:

/**
* Create a new Db instance sharing the current socket connections.
*
* @param {String} dbName the name of the database we want to use.
* @return {Db} a db instance using the new database.
* @api public
*/

so you could do

var serv=mongo.Server("localhost", 27017);
var dbase=mongo.Db("MyDatabase", serv);
var dbase2=dbase.db("MyDatabase2");
Breaking answered 23/8, 2012 at 1:42 Comment(1)
Yes I did see that in the docs here. They just did it in the open callback, but it's using the same object returned from mongo.Db. (They just pass the original object into the callback.) And that is helpful to know, but it seems to make the Server object even less necessary. I'm still just wondering if there's some special purpose for the standalone Server instance. But thanks for the answer. +1Crotchety
S
3

Here is a link to the solution on the mongo docs, for reference. (seems like the same solution the other poster mentioned)

http://mongodb.github.com/node-mongodb-native/markdown-docs/database.html#sharing-the-connections-over-multiple-dbs

The point of separating the connection to the mongo server, and then the DB is for cases like when you want to connect to a ReplSet server, or other custom params. This way, you have a separate process connecting to a mongodb server.

The database connection call is separate simply because of the case you have here: you dont simply want to connect to a mongo server and a single db, but multiple dbs. This separation of connecting to db and server allows this flexibility.

Another Solution: Use node-mongoskin

Mongoskin does what you want to... it allows connecting to server and db all in one command. Not a solution for mongo-native, but worth considering as an alternative library for your future projects.

var mongo = require('mongoskin');
var db = mongo.db('localhost:27017/testDB');
Spermatium answered 29/8, 2012 at 15:38 Comment(3)
The separate database connections could be done even if they didn't have a separate Server constructor. The multiple connections are not done using the Db constructor, but rather the .db property on the instance of the Db constructor. But could you explain what you mean about the ReplSet server? Is it something that is useful without calling the Db constructor?Crotchety
Well, mongo lets you have separate mongodb servers running that replicate data, and you would connect to them all together example: ('192.168.0.1:27017/?auto_reconnect=true','192.168.0.2:27017/?auto_reconnect=true','192.168.0.3:27017/?auto_reconnect=true')Spermatium
updated answer with more info. mentioned mongo-skin which is a wrapper for mongo-native and connects to db and server in one commandSpermatium
R
1

Because these are two separate and distinct actions - you have to connect (or already have a connection) to DB server (computer) in order to query any of the databases on that particular server. You can create distinct database query connections for each of the databases that you will want to use, but at the same time you will be using the same connection to the server.
Most of the time you will NOT want to create a separate server connection for each of the databases (if there are many) because the server usually limits the number of connections.

Rickert answered 15/8, 2012 at 14:17 Comment(1)
But because of the error I get, it seems like I can't create distinct database connections using the same server. It seems like for each database connection, I need to create a new server connection, even if it is the same server. Or am I doing something wrong?Crotchety

© 2022 - 2024 — McMap. All rights reserved.