Challenge - 5 Problems
Result Control Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Understanding result order with find()
Given a MongoDB collection students with documents containing
{name, score}, what will be the order of documents returned by this query?db.students.find({}).limit(3)MongoDB
db.students.find({}).limit(3)Attempts:
2 left
💡 Hint
Without a sort, MongoDB returns documents in natural order.
✗ Incorrect
When no sort is specified, MongoDB returns documents in the order they are stored (natural order). Limit just restricts the number of documents returned.
🧠 Conceptual
intermediate1:30remaining
Why sorting results is important
Why is it important to use
sort() when querying a MongoDB collection if you want consistent results?Attempts:
2 left
💡 Hint
Think about how data is stored and retrieved.
✗ Incorrect
Without sorting, MongoDB returns documents in natural order which can vary. Sorting ensures the order is predictable and consistent.
📝 Syntax
advanced2:00remaining
Correct syntax for sorting and limiting results
Which MongoDB query correctly returns the top 5 students sorted by score descending?
Attempts:
2 left
💡 Hint
Check the order of method chaining and syntax of sort argument.
✗ Incorrect
The correct syntax uses sort with an object specifying field and direction, and limit after sort to get top results.
❓ optimization
advanced2:30remaining
Improving query performance with result control
How does adding an index on the
score field improve the performance of this query?db.students.find().sort({score: -1}).limit(10)Attempts:
2 left
💡 Hint
Think about how indexes help with sorting and filtering.
✗ Incorrect
An index on score lets MongoDB quickly access documents in sorted order, avoiding full collection scans.
🔧 Debug
expert3:00remaining
Why does this query return inconsistent results?
A developer runs this query multiple times:
Sometimes the returned orders differ between runs. What is the most likely reason?
db.orders.find({status: 'pending'}).limit(5)Sometimes the returned orders differ between runs. What is the most likely reason?
Attempts:
2 left
💡 Hint
Consider how MongoDB orders results without explicit sorting.
✗ Incorrect
Without sort(), natural order can vary due to document storage changes, causing inconsistent results.