Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8477523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:29:26+00:00 2026-06-10T18:29:26+00:00

I am just learning mongodb-native driver for nodejs. I connect like this. var mongo=require(mongodb)

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:29:28+00:00Added an answer on June 10, 2026 at 6:29 pm

    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');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just learning MongoDB and MongoMapper. This is on Rails 3. I created a
Just learning the world of jquery, and all my googling gives examples like this:
just learning as3 for flex. i am trying to do this: var someNumber:String =
I'm just learning how to use Mapreduce and reading this documentation: http://django-mongodb.org/topics/mapreduce.html But it
I'm just learning MVC3 now and this is really confusing me. I have a
I'm just learning to use RVM, and would like to know how I can
I am just learning about foreign keys. I am simplifying my tables to this:
I am just learning python and Qt these days. So please consider that this
Im just learning javascript and I'm just wondering why this doesn't work. I've created
I'm learning MongoDB these days. I find that MongoDB doesn't support join. I just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.