Recall & Review
beginner
What does the
sort() method do in MongoDB?The
sort() method orders the documents in a query result by one or more fields, either ascending or descending.Click to reveal answer
beginner
How do you specify ascending order in MongoDB's
sort() method?Use
1 as the value for the field in the sort() method to sort ascending (smallest to largest).Click to reveal answer
beginner
How do you specify descending order in MongoDB's
sort() method?Use
-1 as the value for the field in the sort() method to sort descending (largest to smallest).Click to reveal answer
beginner
Example: How to sort documents by age ascending in MongoDB?
Use
db.collection.find().sort({ age: 1 }) to get documents sorted by the age field from smallest to largest.Click to reveal answer
beginner
Example: How to sort documents by age descending in MongoDB?
Use
db.collection.find().sort({ age: -1 }) to get documents sorted by the age field from largest to smallest.Click to reveal answer
What value do you use in
sort() to sort a field in ascending order?✗ Incorrect
Ascending order is specified by 1 in MongoDB's sort method.
What does
db.collection.find().sort({ score: -1 }) do?✗ Incorrect
Using -1 sorts the documents by the score field in descending order.
Which of these is the correct way to sort by two fields, name ascending and age descending?
✗ Incorrect
You specify each field with 1 for ascending and -1 for descending.
If you want the smallest values first, which sort value do you use?
✗ Incorrect
1 means ascending order, which puts smallest values first.
What happens if you omit the
sort() method in a MongoDB query?✗ Incorrect
Without sort(), MongoDB returns documents in natural order, usually insertion order.
Explain how to use the
sort() method in MongoDB to order query results.Think about how you tell MongoDB which field and direction to sort.
You got /5 concepts.
Describe the difference between ascending and descending sort orders in MongoDB and how to specify each.
Focus on the numeric values used in the sort method.
You got /4 concepts.