Imagine you have a busy coffee shop where many customers come to order drinks. Instead of making a new coffee machine for each customer, you use a few machines shared by everyone. How does this idea relate to connection pooling in MongoDB?
Think about saving time and resources when many users need to connect.
Connection pooling keeps a set of open connections ready to use, so new requests don't need to open a new connection each time. This saves time and system resources.
Given the following MongoDB client code snippet, what is the number of active connections after running it?
const { MongoClient } = require('mongodb'); const uri = 'mongodb://localhost:27017'; const client = new MongoClient(uri, { maxPoolSize: 5 }); async function run() { await client.connect(); const db = client.db('testdb'); const collection = db.collection('items'); // Run 10 queries concurrently await Promise.all( Array.from({ length: 10 }).map(() => collection.findOne({})) ); console.log(client.topology.s.pool.size); await client.close(); } run();
Check the maxPoolSize option and how many connections the pool can open.
The maxPoolSize is set to 5, so the pool will open up to 5 connections to handle the 10 queries concurrently, reusing them as needed.
Select the correct MongoDB connection string that sets the maximum connection pool size to 20.
Look for the official option name for max pool size in MongoDB URI.
The correct URI option to set the maximum connection pool size is maxPoolSize. Other options are invalid or deprecated.
Consider a MongoDB application with many simultaneous users. What is the effect of increasing the maxPoolSize setting?
Think about trade-offs between concurrency and resource consumption.
Increasing maxPoolSize allows more concurrent connections, which can improve throughput under heavy load, but it also uses more memory and CPU resources.
Review the following code snippet. Why might it cause a connection leak in MongoDB?
const { MongoClient } = require('mongodb'); const client = new MongoClient('mongodb://localhost:27017', { maxPoolSize: 10 }); async function query() { await client.connect(); const db = client.db('testdb'); const collection = db.collection('users'); for (let i = 0; i < 100; i++) { collection.findOne({}); } } query();
Check if the client connection is properly closed after use.
The code connects the client but never calls client.close(). This leaves connections open, causing a leak.