0
0
MongoDBquery~5 mins

sort method ascending and descending in MongoDB - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A-1
Btrue
C0
D1
What does db.collection.find().sort({ score: -1 }) do?
ASorts documents by score descending
BSorts documents by score ascending
CFilters documents with score -1
DSorts documents randomly
Which of these is the correct way to sort by two fields, name ascending and age descending?
Asort({ name: 1, age: -1 })
Bsort({ name: -1, age: 1 })
Csort({ name: 0, age: 0 })
Dsort({ name: true, age: false })
If you want the smallest values first, which sort value do you use?
A-1
B1
C0
Dnull
What happens if you omit the sort() method in a MongoDB query?
ADocuments are sorted ascending by default
BDocuments are sorted descending by default
CDocuments are returned in natural order (insertion order)
DQuery will fail
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.