简体   繁体   中英

MongoDB Mongoose state persistence

When you do this in a main.js module:

var db = require('mongoose');
db.connect('mongodb://localhost/piautomation');
var myOwnModule = require('./myOwnModule');

And myOwnModule.js :

var db = require('mongoose');

Is db using a persisted mongoose connection or do you have to wrap the mongoose module in your own module to persist the connection?

Below is the wrapped version.

main.js code:

var dbConnect = require('./dbConnect'),
    myOwnModule = require('./myOwnModule');

dbConnect.js code:

var db = require('mongoose');
module.exports = db.connect('mongodb://localhost/piautomation');

myOwnModule.js code:

var persistedDb = require('./dbConnect');

No, you don't need to do that.

On application start up, you can do the following:

var db = require('mongoose');
db.connect('mongodb://localhost/piautomation');

Connections are pooled internally by mongoose

From the Mongoose Docs

Each connection, whether created with mongoose.connect or mongoose.createConnection are all backed by an internal configurable connection pool defaulting to a size of 5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM