mobgoose will reuse connections to the same url and useDb(database)
for you.
If you are using database-per-tenant multi-tenancy this saves on open connections to the server.
You need to have mongoose
as a dependency.
var mongoose = require('mongoose'),
var mobgoose = require('mobgoose')(mongoose);
// connect via string
mobgoose('mongodb://localhost:27017/my_db');
// connect via options - database overrides value in url
mobgoose({
url: 'mongodb://localhost:27017/my_ignored_db',
database: 'my_other_db',
username: 'user123'
});
These two calls result in a single actual connection to localhost:27017 and each call returns a promise, which is resolved with a 'virtual' connection to my_db and my_other_db respectively.
So you can .then(function(connection) { /* .. */ })
and don't forget, you need to use connection.model(modelName)
to get a model that makes use of said connection, you can't just use a model via require
-- that uses the global connection (nevers established here).
The promise is backed by Bluebird so you can also mobgoose(..).nodeify(callback)
if that's your thing.