最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Aug 15, 2012 at 19:18 gray state is ing asked Aug 15, 2012 at 14:00 gray state is inggray state is ing 2,11711 silver badges20 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3 +200

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./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 mand. 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');

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");

Because these are two separate and distinct actions - you have to connect (or already have a connection) to DB server (puter) 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.

发布评论

评论列表(0)

  1. 暂无评论