Why my NodeJS program opens multiple connections to Mongo (using native driver)

无人久伴 提交于 2019-12-24 03:26:05

问题


From my NodeJS program I connect to Mongodb with native driver. I start mongod and see server waiting for connections. When my program connects I can see however 5 connections instead of one. I don't understand why is happening as I do nothing unusual it seems. Mesage from mongod is below:

2015-05-02T15:35:17.635+1000 [initandlisten] waiting for connections on port 27017
2015-05-02T15:36:17.638+1000 [clientcursormon] mem (MB) res:51 virt:508
2015-05-02T15:36:17.639+1000 [clientcursormon]  mapped (incl journal view):320
2015-05-02T15:36:17.639+1000 [clientcursormon]  connections:0
2015-05-02T15:37:11.594+1000 [initandlisten] connection accepted from 127.0.0.1:52976 #1 (1 connection now open)
2015-05-02T15:37:11.615+1000 [conn1] end connection 127.0.0.1:52976 (0 connections now open)
2015-05-02T15:37:11.625+1000 [initandlisten] connection accepted from 127.0.0.1:52977 #2 (1 connection now open)
2015-05-02T15:37:11.626+1000 [initandlisten] connection accepted from 127.0.0.1:52978 #3 (2 connections now open)
2015-05-02T15:37:11.627+1000 [initandlisten] connection accepted from 127.0.0.1:52979 #4 (3 connections now open)
2015-05-02T15:37:11.628+1000 [initandlisten] connection accepted from 127.0.0.1:52980 #5 (4 connections now open)
2015-05-02T15:37:11.628+1000 [initandlisten] connection accepted from 127.0.0.1:52981 #6 (5 connections now open)

This is my NodeJS code, I stripped out some parts of it for clarity:

MongoClient.connect(mongodb_connection_string, function (err, db) {
    var collections = {};
    collections.users = db.collection("users");
    collections.disciplines = db.collection("disciplines");
    var users = require("./models/users"),
        disciplines = require("./models/disciplines");
    users.setDb(collections);
    disciplines.setDb(collections);
    app.use(session({
        secret: 'keyboard cat',
        store: new MongoStore({
            db: db,
            ttl: 2 // in minutes
            // ttl: 14 * 24 * 60 * 60 // 14 days
        }),
        saveUninitialized: false,
        resave: true
    }));
    app.all("/*", function (req, res, next) {
        res.sendfile("index.html", {
            root: __dirname + "/public"
        });
    });
    var server = app.listen(server_port, server_ip_address, function () {});
});

When I connect from mongo console I got one connection only. Is Nodejs native mongo driver maintaining some pool of connection maybe? Thats my best guess, otherwise not sure what I'm doing wrong.


回答1:


From the docs https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html

Connection pool configuration:
maxPoolSize=n: The maximum number of connections in the connection pool
Default value is 5



回答2:


This is because of Connection pool configuration

maxPoolSize=n: The maximum number of connections in the connection pool Default value is 5

If you want to change maxPoolSize value pass this as URL parameter in connection string:

var connection_string = "mongodb://localhost/mydb?maxPoolSize=50"
MongoClient.connect(connection_string, function (err, db) {


来源:https://stackoverflow.com/questions/29998964/why-my-nodejs-program-opens-multiple-connections-to-mongo-using-native-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!