0
0
Expressframework~20 mins

Finding and querying documents in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Express route when querying documents?

Consider this Express route that queries a MongoDB collection for users with age greater than 25:

app.get('/users', async (req, res) => {
  const users = await User.find({ age: { $gt: 25 } });
  res.json(users);
});

What will the response contain?

Express
app.get('/users', async (req, res) => {
  const users = await User.find({ age: { $gt: 25 } });
  res.json(users);
});
AA JSON array of all user documents regardless of age
BA JSON array of user documents where each user's age is greater than 25
CA JSON object with a single user document where age equals 25
DAn error because $gt is not a valid query operator
Attempts:
2 left
💡 Hint

Think about what the MongoDB $gt operator does in a query.

📝 Syntax
intermediate
2:00remaining
Which option correctly queries documents with multiple conditions in Express?

You want to find all products with price less than 100 and category 'books'. Which query is correct?

Express
const products = await Product.find( /* your query here */ );
A{ price: { $lt: 100 }, category: 'books' }
B{ price: { $lt: 100 } && category: 'books' }
C{ price: { $lt: 100 }, category == 'books' }
D{ price: { $lt: 100 }, category: ['books'] }
Attempts:
2 left
💡 Hint

Remember how to write multiple conditions in a MongoDB query object.

🔧 Debug
advanced
2:00remaining
Why does this Express query return an empty array unexpectedly?

Given this code:

app.get('/search', async (req, res) => {
  const term = req.query.term;
  const results = await Item.find({ name: { $regex: term } });
  res.json(results);
});

Users report no results even when matching items exist. What is the likely cause?

Express
app.get('/search', async (req, res) => {
  const term = req.query.term;
  const results = await Item.find({ name: { $regex: term } });
  res.json(results);
});
AThe query should use $text instead of $regex for string matching
BThe $regex operator is invalid in MongoDB queries
CThe regex is case-sensitive by default, so matches may fail if case differs
DThe term variable is undefined because req.query.term is missing
Attempts:
2 left
💡 Hint

Think about how MongoDB regex queries handle letter case.

state_output
advanced
2:00remaining
What is the value of 'count' after this Express route runs?

Consider this route that counts documents matching a condition:

app.get('/count', async (req, res) => {
  const count = await Order.countDocuments({ status: 'shipped' });
  res.send(`Shipped orders: ${count}`);
});

If there are 7 orders with status 'shipped', what will be sent in the response?

Express
app.get('/count', async (req, res) => {
  const count = await Order.countDocuments({ status: 'shipped' });
  res.send(`Shipped orders: ${count}`);
});
AShipped orders: 7
BShipped orders: undefined
CShipped orders: 0
DAn error because countDocuments is not a function
Attempts:
2 left
💡 Hint

Recall what countDocuments returns when documents match the query.

🧠 Conceptual
expert
3:00remaining
Which option best explains the difference between find() and findOne() in Express MongoDB queries?

When querying MongoDB in Express, what is the key difference between find() and findOne() methods?

A<code>find()</code> requires a callback; <code>findOne()</code> only supports promises
B<code>find()</code> returns a single document; <code>findOne()</code> returns an array of documents
C<code>find()</code> modifies documents; <code>findOne()</code> only reads documents
D<code>find()</code> returns an array of all matching documents; <code>findOne()</code> returns the first matching document or null
Attempts:
2 left
💡 Hint

Think about the return types of these methods.