Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 375
gray state is coming Avatar asked Aug 15 '12 14:08

gray state is coming


People also ask

How does MongoDB driver work?

The official MongoDB Node. js driver allows Node. js applications to connect to MongoDB and work with data. The driver features an asynchronous API which allows you to interact with MongoDB using Promises or via traditional callbacks.

Should I use mongoose or MongoDB driver?

On the downside, learning mongoose can take some time, and has some limitations in handling schemas that are quite complex. However, if your collection schema is unpredictable, or you want a Mongo-shell like experience inside Node. js, then go ahead and use the mongodb driver. It is the simplest to pick up.

How do I access MongoDB over HTTP on the native driver port?

MongoDB has an uncomplicated web-based central port at 28017 by default. At the default port of 27017, there is no HTTP access. The error port is used for native driver access, not HTTP traffic. To obtain MongoDB, you'll need to use a driver like the MongoDB essential driver for NodeJS.


2 Answers

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");
like image 36
ebohlman Avatar answered Oct 15 '22 22:10

ebohlman


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');
like image 135
Kostia Avatar answered Oct 15 '22 21:10

Kostia