Recall & Review
beginner
What does sorting by multiple fields mean in MongoDB?
It means arranging documents in order based on more than one field, for example, first by age, then by name.
Click to reveal answer
beginner
How do you specify sorting order for multiple fields in MongoDB?
You provide an object to
sort() with field names as keys and 1 for ascending or -1 for descending as values, like { age: 1, name: -1 }.Click to reveal answer
beginner
What happens if two documents have the same value in the first sorted field?
MongoDB uses the next field in the sort order to decide which document comes first.
Click to reveal answer
intermediate
Write a MongoDB query to sort users first by
score descending, then by username ascending.db.users.find().sort({ score: -1, username: 1 })Click to reveal answer
beginner
Why is sorting by multiple fields useful in real life?
It helps organize data clearly, like sorting a contact list by last name and then first name, so people with the same last name are grouped and ordered by first name.
Click to reveal answer
In MongoDB, how do you sort documents by two fields,
age ascending and name descending?✗ Incorrect
Use 1 for ascending and -1 for descending in the sort object.
What does the number -1 mean in a MongoDB sort object?
✗ Incorrect
In MongoDB, -1 means descending order.
If two documents have the same value in the first sorted field, what does MongoDB do?
✗ Incorrect
MongoDB uses the next field to break ties.
Which of these is a valid MongoDB sort command to sort by
price ascending and rating descending?✗ Incorrect
Use 1 for ascending and -1 for descending in the sort object.
Why might you want to sort by multiple fields in a database?
✗ Incorrect
Sorting by multiple fields helps organize data better when one field is not enough.
Explain how to sort documents by multiple fields in MongoDB and why it is useful.
Think about sorting by age then name.
You got /4 concepts.
Write a MongoDB query to sort a collection by two fields: first descending by
date, then ascending by title.Use 1 for ascending and -1 for descending.
You got /2 concepts.