0
0
Node.jsframework~20 mins

MongoDB connection with mongodb driver in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB Connection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this MongoDB connection code?

Consider the following Node.js code snippet using the mongodb driver to connect to a MongoDB server and list database names. What will be printed to the console?

Node.js
import { MongoClient } from 'mongodb';

async function listDatabases() {
  const client = new MongoClient('mongodb://localhost:27017');
  try {
    await client.connect();
    const databasesList = await client.db().admin().listDatabases();
    console.log(databasesList.databases.map(db => db.name));
  } finally {
    await client.close();
  }
}

listDatabases();
AAn error because the connection string is missing credentials
BAn array of database names like ['admin', 'local', 'test']
CAn empty array [] because no databases exist
DA TypeError because listDatabases is not a function
Attempts:
2 left
💡 Hint

Think about what listDatabases() returns and what map(db => db.name) does.

📝 Syntax
intermediate
2:00remaining
Which option correctly creates a MongoClient instance to connect to a MongoDB Atlas cluster?

You want to connect to a MongoDB Atlas cluster with the connection string mongodb+srv://user:pass@cluster0.mongodb.net. Which code snippet correctly creates the MongoClient instance?

Aconst client = new MongoClient('mongodb+srv://user:pass@cluster0.mongodb.net', { useNewUrlParser: true });
Bconst client = new MongoClient('mongodb://user:pass@cluster0.mongodb.net');
Cconst client = new MongoClient('mongodb+srv://user:pass@cluster0.mongodb.net', true);
Dconst client = new MongoClient('mongodb+srv://user:pass@cluster0.mongodb.net');
Attempts:
2 left
💡 Hint

Remember the correct URI scheme for Atlas and the constructor signature.

optimization
advanced
2:00remaining
How to optimize MongoDB connection reuse in a Node.js app?

You have a Node.js app that connects to MongoDB on every request, causing delays. Which approach best optimizes connection reuse?

ACreate a single <code>MongoClient</code> instance and reuse it across requests
BCreate a new <code>MongoClient</code> instance for each request and close it after
CUse <code>MongoClient.connect()</code> inside each request handler without closing
DUse a global variable to store the connection string and create clients as needed
Attempts:
2 left
💡 Hint

Think about connection overhead and resource management.

🔧 Debug
advanced
2:00remaining
Why does this MongoDB connection code throw a timeout error?

Given this code snippet, why does it throw a timeout error when trying to connect?

import { MongoClient } from 'mongodb';

const client = new MongoClient('mongodb://invalidhost:27017');

async function run() {
  await client.connect();
  console.log('Connected');
}

run();
AThe hostname 'invalidhost' does not resolve, causing connection timeout
BThe connection string is missing username and password, causing timeout
CThe port 27017 is blocked by firewall, causing timeout
DThe MongoClient constructor is missing options, causing timeout
Attempts:
2 left
💡 Hint

Check if the hostname is reachable.

🧠 Conceptual
expert
2:00remaining
What is the role of the useUnifiedTopology option in MongoClient?

In older versions of the mongodb driver, the useUnifiedTopology option was recommended. What does enabling this option do?

AForces the driver to use legacy connection methods for compatibility
BDisables connection pooling to reduce resource usage
CEnables the new unified topology layer that improves server discovery and monitoring
DAutomatically retries failed operations without user intervention
Attempts:
2 left
💡 Hint

Think about how the driver manages connections and monitors servers.