Bird
Raised Fist0
MongoDBquery~20 mins

Connection pooling concept in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Connection Pooling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the main benefit of connection pooling in MongoDB?

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?

AIt increases the number of connections to the database indefinitely.
BIt reduces the time to establish new connections by reusing existing ones.
CIt stores data temporarily to speed up queries.
DIt encrypts all data sent between client and server.
Attempts:
2 left
💡 Hint

Think about saving time and resources when many users need to connect.

❓ query_result
intermediate
2:00remaining
How many connections are active after running this MongoDB connection pool code?

Given the following MongoDB client code snippet, what is the number of active connections after running it?

MongoDB
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();
A5
B10
C1
D0
Attempts:
2 left
💡 Hint

Check the maxPoolSize option and how many connections the pool can open.

📝 Syntax
advanced
2:00remaining
Which MongoDB connection string correctly enables connection pooling with a max pool size of 20?

Select the correct MongoDB connection string that sets the maximum connection pool size to 20.

Amongodb://localhost:27017/?connectionPool=20
Bmongodb://localhost:27017/?poolSize=20
Cmongodb://localhost:27017/?maxConnections=20
Dmongodb://localhost:27017/?maxPoolSize=20
Attempts:
2 left
💡 Hint

Look for the official option name for max pool size in MongoDB URI.

❓ optimization
advanced
2:00remaining
How does increasing maxPoolSize affect MongoDB application performance?

Consider a MongoDB application with many simultaneous users. What is the effect of increasing the maxPoolSize setting?

AIt can improve throughput by allowing more concurrent connections but may increase resource usage.
BIt disables connection pooling entirely.
CIt reduces memory usage by limiting connections.
DIt always decreases latency regardless of workload.
Attempts:
2 left
💡 Hint

Think about trade-offs between concurrency and resource consumption.

🔧 Debug
expert
2:00remaining
Why does this MongoDB connection pool code cause a connection leak?

Review the following code snippet. Why might it cause a connection leak in MongoDB?

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();
AThe maxPoolSize is too low to handle 100 queries.
BThe findOne method is called without await, so queries never complete.
CThe code never closes the client connection, causing connections to remain open.
DThe MongoClient constructor is missing the poolSize option.
Attempts:
2 left
💡 Hint

Check if the client connection is properly closed after use.

Practice

(1/5)
1. What is the main purpose of connection pooling in MongoDB?
easy
A. To reuse existing database connections for faster performance
B. To create a new connection for every database request
C. To store data temporarily in the database
D. To backup the database automatically

Solution

  1. Step 1: Understand connection pooling concept

    Connection pooling means reusing open connections instead of opening new ones each time.
  2. Step 2: Identify the benefit of reusing connections

    Reusing connections saves time and resources, making database access faster.
  3. Final Answer:

    To reuse existing database connections for faster performance -> Option A
  4. Quick Check:

    Connection pooling = reuse connections [OK]
Hint: Connection pooling means reuse connections, not create new ones [OK]
Common Mistakes:
  • Thinking connection pooling creates new connections each time
  • Confusing connection pooling with data caching
  • Assuming connection pooling is for backup
2. Which of the following is the correct way to set the maximum pool size in a MongoDB connection string?
easy
A. mongodb://localhost:27017/?poolMax=50
B. mongodb://localhost:27017/?maxConnections=50
C. mongodb://localhost:27017/?maxPoolSize=50
D. mongodb://localhost:27017/?connectionLimit=50

Solution

  1. Step 1: Recall MongoDB connection string options

    The correct option to set max pool size is maxPoolSize.
  2. Step 2: Match the option with the connection string

    Only mongodb://localhost:27017/?maxPoolSize=50 uses maxPoolSize correctly in the connection string.
  3. Final Answer:

    mongodb://localhost:27017/?maxPoolSize=50 -> Option C
  4. Quick Check:

    maxPoolSize sets max connections [OK]
Hint: Use maxPoolSize to set max connections in MongoDB URI [OK]
Common Mistakes:
  • Using maxConnections instead of maxPoolSize
  • Using poolMax or connectionLimit which are invalid
  • Placing options outside the URI query string
3. Consider this MongoDB connection code snippet using Node.js driver:
const client = new MongoClient(uri, { maxPoolSize: 5 });
await client.connect();

const db = client.db('test');
const col = db.collection('items');

// Simulate 10 quick queries
for (let i = 0; i < 10; i++) {
  await col.findOne({});
}
What is the effect of setting maxPoolSize to 5 here?
medium
A. The pool size limits the number of documents returned
B. Only 5 connections will be open at once, reused for all queries
C. No connections will be reused, each query opens a new one
D. 10 connections will open, one per query

Solution

  1. Step 1: Understand maxPoolSize effect on connections

    maxPoolSize limits how many connections can be open simultaneously in the pool.
  2. Step 2: Analyze the loop with 10 queries

    Even with 10 queries, only 5 connections open and are reused for all queries.
  3. Final Answer:

    Only 5 connections will be open at once, reused for all queries -> Option B
  4. Quick Check:

    maxPoolSize=5 means max 5 open connections [OK]
Hint: maxPoolSize limits open connections, not queries [OK]
Common Mistakes:
  • Thinking each query opens a new connection
  • Confusing pool size with query limits
  • Assuming pool size affects returned data count
4. You wrote this code to connect to MongoDB with connection pooling:
const client = new MongoClient(uri, { maxPoolSize: 10 });
await client.connect();

// Later in code
await client.close();
await client.connect();
But you notice performance drops after reconnecting. What is the likely problem?
medium
A. The URI string is missing maxPoolSize parameter
B. maxPoolSize is too low, causing slow queries
C. You must create a new MongoClient instance before reconnecting
D. Calling client.close() closes all connections, so reconnecting is slow

Solution

  1. Step 1: Understand client.close() effect

    Calling client.close() closes all connections in the pool, clearing them.
  2. Step 2: Analyze reconnecting after close()

    Reconnecting after close() requires opening new connections, causing delay and performance drop.
  3. Final Answer:

    Calling client.close() closes all connections, so reconnecting is slow -> Option D
  4. Quick Check:

    client.close() clears pool, reconnect is slower [OK]
Hint: Avoid closing client if you plan to reconnect soon [OK]
Common Mistakes:
  • Assuming maxPoolSize affects reconnect speed
  • Thinking you can reconnect without new client instance
  • Ignoring that close() clears all connections
5. Your app makes many short database requests to MongoDB. You want to optimize performance using connection pooling. Which approach is best?
hard
A. Create one MongoClient with a suitable maxPoolSize and reuse it for all requests
B. Create a new MongoClient for each request with maxPoolSize=1
C. Set maxPoolSize to 1 and open a new connection for each request
D. Disable connection pooling to avoid connection reuse

Solution

  1. Step 1: Understand connection pooling best practice

    Best practice is to create one MongoClient instance and reuse it to benefit from pooling.
  2. Step 2: Evaluate options for many short requests

    Reusing one client with a proper maxPoolSize allows multiple connections reused efficiently.
  3. Final Answer:

    Create one MongoClient with a suitable maxPoolSize and reuse it for all requests -> Option A
  4. Quick Check:

    Reuse one client with pooling for many requests [OK]
Hint: Reuse one client with maxPoolSize for many requests [OK]
Common Mistakes:
  • Creating new client per request wastes resources
  • Setting maxPoolSize to 1 limits concurrency
  • Disabling pooling reduces performance
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard