How to share the connection pool across multiple databases
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
Solution: How to share the connection pool across multiple databases
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 =
Trust Score
2 verifications
- 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
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
var mongoose = require('mongoose');
var db1 = mongoose.createConnection('mongodb://127.0.0.1/test1'); var sites1 = db1.model('site', new mongoose.Schema({ … }));
- 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
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep