0
0
MongoDBquery~10 mins

sort method ascending and descending in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - sort method ascending and descending
Start with collection
Apply sort method
Specify field and order
MongoDB sorts documents
Return sorted documents
The sort method takes a field and order (1 for ascending, -1 for descending) and returns documents sorted accordingly.
Execution Sample
MongoDB
db.products.find().sort({ price: 1 })
db.products.find().sort({ price: -1 })
This code sorts the 'products' collection by 'price' in ascending and descending order.
Execution Table
StepQuerySort FieldSort OrderResult Order
1db.products.find().sort({ price: 1 })price1 (ascending)[10, 20, 30, 40, 50]
2db.products.find().sort({ price: -1 })price-1 (descending)[50, 40, 30, 20, 10]
3End--Sorting complete, documents returned
💡 Sorting ends after documents are returned in requested order.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
products[{price:30},{price:10},{price:50},{price:20},{price:40}][{price:10},{price:20},{price:30},{price:40},{price:50}][{price:50},{price:40},{price:30},{price:20},{price:10}]Sorted arrays returned
Key Moments - 2 Insights
Why does using 1 sort ascending and -1 sort descending?
In the execution_table rows 1 and 2, 1 means ascending order (smallest to largest), and -1 means descending order (largest to smallest).
Does sort change the original collection data?
No, as shown in variable_tracker, the original 'products' array stays the same; sort returns a new ordered list.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 1, what is the order of prices after sorting ascending?
A[50, 40, 30, 20, 10]
B[10, 20, 30, 40, 50]
C[30, 10, 50, 20, 40]
D[20, 10, 40, 30, 50]
💡 Hint
Check the 'Result Order' column in row 1 of execution_table.
At which step does the sort order become descending?
AStep 1
BStep 3
CStep 2
DNo descending sort
💡 Hint
Look at the 'Sort Order' column in execution_table rows.
If we change sort({ price: 1 }) to sort({ price: -1 }), how does the variable 'products' change after step 1?
AIt becomes ascending order
BIt becomes descending order
CIt stays unsorted
DIt becomes empty
💡 Hint
Refer to variable_tracker after Step 1 and Step 2 for sorted arrays.
Concept Snapshot
MongoDB sort method:
- Use sort({field: 1}) for ascending order
- Use sort({field: -1}) for descending order
- Returns documents sorted by the field
- Does not modify original data
- Chain after find() to sort query results
Full Transcript
The MongoDB sort method orders documents by a specified field. Using 1 sorts ascending (smallest to largest), and -1 sorts descending (largest to smallest). The method is chained after find() to return sorted results without changing the original collection. For example, sorting products by price ascending returns prices from lowest to highest. Sorting descending reverses this order. This visual shows step-by-step how the sort method changes the order of documents returned.