FG
🗄️ DatabasesMongoDB

How to share the connection pool across multiple databases

Freshover 12 years ago
Mar 14, 20260 views
Confidence Score88%
88%

Problem

In node-mongodb-native, I can share the connection pool across multiple databases: <code> var mongodb = require("mongodb"), mongoserver = new mongodb.Server(host, port, server_options), db_connector = new mongodb.Db(name, mongoserver, db_options); db_connector.open(callback); </code> And then I use <code>db_connector.db(name)</code>, this returns a new db instance that shares the connections off the previous instance but will send all commands to the database name . This allows for better control of resource usage in a multiple database scenario. HOW CAN I DO THIS IN MONGOOSE?

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Moderate Confidence Fix
84% confidence100% success rate2 verificationsLast verified Mar 14, 2026

Solution: How to share the connection pool across multiple databases

Low Risk

O.k... I have just gotten my case to work - though I haven't checked for the amount of connections... It can be hard to find useful examples in the documentation, but with a days worth of tinkering, testing and being stubborn as a donkey, I have finally figured out, how to make it work... So in case anybody else needs this, this is what works for me now (in it's most basic form): var mongoose =

84

Trust Score

2 verifications

100% success
  1. 1

    O.k... I have just gotten my case to work - though I haven't checked for the amo

    It can be hard to find useful examples in the documentation, but with a days worth of tinkering, testing and being stubborn as a donkey, I have finally figured out, how to make it work...

  2. 2

    So in case anybody else needs this, this is what works for me now (in it's most

    So in case anybody else needs this, this is what works for me now (in it's most basic form):

  3. 3

    var mongoose = require('mongoose');

    var db1 = mongoose.createConnection('mongodb://127.0.0.1/test1'); var sites1 = db1.model('site', new mongoose.Schema({ … }));

  4. 4

    var db2 = mongoose.createConnection('mongodb://127.0.0.1/test2');

    var sites2 = db2.model('site', new mongoose.Schema({ … }));

Validation

Resolved in Automattic/mongoose GitHub issue #1124. Community reactions: 1 upvotes.

Verification Summary

Worked: 2
Last verified Mar 14, 2026

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

mongoosemongodbodmnew-feature