0
0
MongoDBquery~20 mins

Connection pooling concept in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 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.