Node.js Mongodb-native driver connection sharing

后端 未结 2 2050
-上瘾入骨i
-上瘾入骨i 2021-02-11 04:48

main.js

var http = require(\'http\');
var UserModel = require(\'./models/user.js\');
var server = http.createServer(function(req, res){         


        
2条回答
  •  别跟我提以往
    2021-02-11 05:12

    I'm currently using a global connection with multiple http requests. In the past I created a complex library that was creating several connections to MongoDB and picking up one randomly for each connection.

    Later I found that the native driver can do that for me, which is pretty neat. Currently I'm using a single object, and the driver chooses to which connection send the query.

    var srvOpts = {
        auto_reconnect: true,
        poolSize: 10,
    };
    
    var conn = new Mongo.Server("localhost", 27017, srvOpts),
        db = new Mongo.Db("dbname", conn, {});
    
    db.open(function (){});
    

    As you can this is a great idea, I'm thinking to copy that idea into the Redis driver that I'm using, but I'm short on time so I doubt that I will do any time soon.

提交回复
热议问题