0
0
MongoDBquery~20 mins

Sorting by multiple fields in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sorting Mastery in MongoDB
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Sorting documents by age ascending then name descending
Given a collection people with documents containing name and age, what is the output of this query sorting by age ascending and name descending?
MongoDB
db.people.find().sort({age: 1, name: -1})
A[{name: "Zara", age: 25}, {name: "Anna", age: 25}, {name: "Bob", age: 30}]
B[{name: "Bob", age: 30}, {name: "Anna", age: 25}, {name: "Zara", age: 25}]
C[{name: "Anna", age: 25}, {name: "Zara", age: 25}, {name: "Bob", age: 30}]
D[{name: "Anna", age: 25}, {name: "Bob", age: 30}, {name: "Zara", age: 25}]
Attempts:
2 left
💡 Hint
Remember ascending means smallest to largest, descending means reverse alphabetical order.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in sorting by multiple fields
Which option contains a syntax error in the MongoDB sort method for sorting by score descending and date ascending?
MongoDB
db.records.find().sort({score: -1, date: 1})
Adb.records.find().sort({score: -1, date: 1})
Bdb.records.find().sort({score: -1; date: 1})
C)}1 :etad ,1- :erocs{(tros.)(dnif.sdrocer.bd
Ddb.records.find().sort({score: -1 date: 1})
Attempts:
2 left
💡 Hint
Check the punctuation between fields inside the sort object.
optimization
advanced
2:30remaining
Optimizing sorting on multiple fields with indexes
Which index will optimize the query db.orders.find().sort({customerId: 1, orderDate: -1}) best?
AAn index on {orderDate: -1, customerId: 1}
BAn index on {orderDate: 1, customerId: 1}
CAn index on {customerId: 1, orderDate: -1}
DAn index on {customerId: -1, orderDate: 1}
Attempts:
2 left
💡 Hint
Indexes should match the sort fields and their order/direction.
🔧 Debug
advanced
2:30remaining
Why does this multi-field sort not work as expected?
A developer runs db.products.find().sort({category: 1, price: 1}) but the results are not sorted by price within each category. What is the likely cause?
AThe collection has no index on category and price, causing inefficient sorting.
BThe sort object keys are unordered, so MongoDB ignores the second field.
CMongoDB sorts only by the first field in the sort object, ignoring others.
DThe query needs a projection to include category and price for sorting.
Attempts:
2 left
💡 Hint
Think about how MongoDB uses indexes to optimize sorting.
🧠 Conceptual
expert
3:00remaining
Understanding sort order precedence in MongoDB
If you run db.logs.find().sort({level: -1, timestamp: 1}), which statement best describes the sorting behavior?
ADocuments are sorted randomly because mixed sort directions are not allowed.
BDocuments are sorted by timestamp ascending only, ignoring level.
CDocuments are sorted by level ascending, then timestamp descending.
DDocuments are sorted first by level descending, then by timestamp ascending within each level.
Attempts:
2 left
💡 Hint
The order of fields in the sort object defines precedence.