0
votes

I'm new to mongodb and node.js, and confused about connection pooling. Suppose the code is like this:

import mongodb from "mongodb";
const { MongoClient } = mongodb;

const client = new MongoClient("localhost:27017");

async function main() {
  await client.connect();
}

According to the driver documentation, MongoClient's has a connection pool of size 5 by default.

So every call await client.connect(); creates 1 connection to mongod, and if I call await client.connect() 6 times, the 6th connection can't be established?

1

1 Answers

1
votes

The connection pool is per server that the client is aware of. Calling connect multiple times doesn't make the client aware of any more servers and hence doesn't affect the number of connections in the pool.

Separately, the client contains one monitoring connection per known server, such that in a typical replica set deployment a client that doesn't perform any application-issued operations would have 3 open connections, not 1. In sharded clusters the number of monitoring connections is the same as number of mongos processes.

Similarly to this, if you are connecting to a 3-node replica set with min connection pool size set to 5, the client will establish 15 application connections (5 per each RS node) in addition to the 3 monitoring connections.