Challenge - 5 Problems
MongoDB Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Sort documents by age ascending
Given a collection
Assume the collection has:
users with documents containing name and age, what is the output of this query?db.users.find().sort({age: 1})Assume the collection has:
{name: 'Alice', age: 30}{name: 'Bob', age: 25}{name: 'Carol', age: 35}MongoDB
db.users.find().sort({age: 1})Attempts:
2 left
💡 Hint
Sorting by age with 1 means ascending order.
✗ Incorrect
The sort method with {age: 1} sorts documents by age from smallest to largest.
❓ query_result
intermediate2:00remaining
Sort documents by age descending
Using the same
With documents:
users collection, what is the output of this query?db.users.find().sort({age: -1})With documents:
{name: 'Alice', age: 30}{name: 'Bob', age: 25}{name: 'Carol', age: 35}MongoDB
db.users.find().sort({age: -1})Attempts:
2 left
💡 Hint
Sorting by age with -1 means descending order.
✗ Incorrect
The sort method with {age: -1} sorts documents by age from largest to smallest.
🧠 Conceptual
advanced1:30remaining
Understanding sort order values
In MongoDB, what do the values 1 and -1 mean when used in the
sort() method?Attempts:
2 left
💡 Hint
Think about smallest to largest and largest to smallest.
✗ Incorrect
In MongoDB, 1 sorts ascending (smallest to largest), -1 sorts descending (largest to smallest).
📝 Syntax
advanced1:30remaining
Identify the invalid sort syntax
Which of the following MongoDB sort method calls will cause a syntax error?
Attempts:
2 left
💡 Hint
Sort values must be numeric 1 or -1.
✗ Incorrect
MongoDB sort values must be 1 or -1. Using 'asc' as a string causes an error.
❓ optimization
expert2:30remaining
Optimizing sort with index
You have a large MongoDB collection with an index on
age. Which query will use the index efficiently for sorting by age descending?Attempts:
2 left
💡 Hint
Index on age supports sorting by age ascending or descending.
✗ Incorrect
Sorting by age descending matches the index on age and uses it efficiently.