Multiple node-mongodb-native connections

后端 未结 1 1059
Happy的楠姐
Happy的楠姐 2021-01-24 03:39

When I run this Node.js code:

var mongodb = require(\'mongodb\'),
  MongoClient = mongodb.MongoClient;
MongoClient.connect(\'mongodb://localhost:27017/mydb\', fu         


        
1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-24 04:23

    Sure. MongoClient uses the connection pools option from the node native driver. This is actually really a Server Object and the number of connections is 5 by default.

    You can override the setting like this:

    var async = require('async'),
        mongo = require('mongo'),
        MongoClient = mongo.MongoClient;
    
    
    MongoClient.connect('mongodb://localhost/test',{ server: { poolSize: 1  }},function(err,db) {
    
    
    });
    

    So setting "poolSize" in the server options specifies the number of connections used in the pool. Best to stick with the default or higher really though.

    0 讨论(0)
提交回复
热议问题