According to [connPoolStats][1]
this command .`connPoolStats`. returns information regarding the open outgoing connections from the current database instance to other members of the sharded cluster or replica set.
db.runCommand( { "connPoolStats" : 1 } )
The value of the argument (i.e. 1 ) does not affect the output of your the command.
According to [connection pool options][2]
The maximum number of connections in the connection pool.
**The default value is 100**.
According to this it can be done [deep-dive-into-connection-pooling][3]
var express = require('express');
var mongodb = require('mongodb');
var app = express();
var MONGODB_URI = 'mongo-uri';
app.get('/', function(req, res) {
// BAD! Creates a new connection pool for every request
mongodb.MongoClient.connect(MONGODB_URI, function(err, db) {
if(err) throw err;
var coll = db.collection('test');
coll.find({}, function(err, docs) {
docs.each(function(err, doc) {
if(doc) {
res.write(JSON.stringify(doc) + "\n");
}
else {
res.end();
}
});
});
});
});
// App may initialize before DB connection is ready
app.listen(3000);
console.log('Listening on port 3000');
[1]: https://docs.mongodb.com/manual/reference/command/connPoolStats/
[2]: https://docs.mongodb.com/manual/reference/connection-string/#connection-pool-options
[3]: https://blog.mlab.com/2013/11/deep-dive-into-connection-pooling/
There are many ways this could be tweaked to accept configuration objects in places, but overall it's similar to how you have your code laid out, albeit with more modern JS syntax. Could easily be rewritten to prototypes and callbacks, if that's your requirement.
mongo.js
const { MongoClient } = require('mongodb');
const config = require('./config');
const Users = require('./Users');
const conf = config.get('mongodb');
class MongoBot {
constructor() {
const url = `mongodb://${conf.hosts.join(',')}`;
this.client = new MongoClient(url, conf.opts);
}
async init() {
await this.client.connect();
console.log('connected');
this.db = this.client.db(conf.db);
this.Users = new Users(this.db);
}
}
module.exports = new MongoBot();
Users.js
class User {
constructor(db) {
this.collection = db.collection('users');
}
async addUser(user) {
const newUser = await this.collection.insertOne(user);
return newUser;
}
}
module.exports = User;
app.js
const mongo = require('./mongo');
async function start() {
// other app startup stuff...
await mongo.init();
// other app startup stuff...
}
start();
someFile.js
const { Users } = require('./mongo');
async function someFunction(userInfo) {
const user = await Users.addUser(userInfo);
return user;
}