useDB and connection pooling

I’m working on establishing a connection to MongoDB within an AWS Lambda function.

My question is whether the following code snippet effectively utilizes connection pooling, or if it always creates a single connection:

`JavaScript// Creating a MongoClient with a pool size
const client = await new MongoClient(uri, { minPoolSize: 10 }).connect();

// Switching to test database
connection = await mongoose.createConnection().setClient(client).useDB(‘test’);

const promises = ;
for (let i = 0; i < 5; i++) {
// Will this use a new connection from the pool or reuse an existing one?
promises.push(connection.model(“Message”).create(/* your message data */));
await Promise.all(promises);`

Hi Rohit,

Welcome to the MongoDB forums!

MongoClient will manage your connection pool for you. Therefore, it will try to reuse the existing connections.

You can test this out for yourself. If you’re running on MongoDB Atlas (you can try it out for free), you can see the number of simultaneous connections that are active on your server right there on the UI.

I’ve changed your code sample to run 5000 times instead of 5, and you can see that the number of active connections is very stable on this screenshot. :slight_smile:

Let us know if you need anything else!

Thanks a lot Joel, will try it out and let you know for any further clarifications