0

With MongoDB, a suggestion was to always re-use the same database connection, and have a pool of connections to support some concurrency.

In node-mongodb-native 1.x you could configure the db and server object, creating a pool like so:

var server = new Server(
    config.host,
    config.port,
    {
        auto_reconnect  : true,
        poolSize        : 5    // <= here is the pool
    }
);

var db = new Db(
    config.database,
    server
);

db.open(function(err, db) {
    // ...
}

In 2.0 they deprecated everything except MongoClient for connecting:

MongoClient.connect(URI, callback);

Where do I add the pool options? Do I have a pool now automatically?

With 2.1 they go a step further and suggest using a generator: https://mongodb.github.io/node-mongodb-native/2.1/reference/ecmascript6/connecting/

Is this effectively using a separate connection for every action again? Is using pools obsolete?

Redsandro
  • 11,060
  • 13
  • 76
  • 106

1 Answers1

1

MongoClient.connect takes an optional options parameter with a server field that lets you set the size of the connection pool:

const options = {
    server: {
        poolSize: 10
    }
};
MongoClient.connect(url, options, callback);

If you don't specify it, the default poolSize is 5. The server options are documented here.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471