Reputation: 1011
I want to make connection pooling on MongoDB. I am on node environment and using mongoose package for interaction with MongoDB. I am able to interact with one instance of MongoDB.
How I can interact with two instance of MongoDB?
Two instances will be on different port
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var mongo = require('mongodb');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://127.0.0.1/db1', { useMongoClient: true });
var db = mongoose.connection;
This is my Implementation of interacting with MongoDB one instance.
Upvotes: 1
Views: 1604
Reputation: 23515
There is two way to connect to mongodb database using mongoose.
The one you are using
mongoose.connect('mongodb://127.0.0.1/db1', { useMongoClient: true });
var db = mongoose.connection;
The connection
object is stored directly into the mongoose package
.
The other
const connection1 = mongoose.createConnection(url, opt);
const connection2 = mongoose.createConnection(url, opt);
connection1.once('open', () => {
// We are connected
});
connection2.once('open', () => {
// We are connected
});
Where the connections objects are handled directly by yourself
See the Multiple connections part in mongoose documentation
Upvotes: 1