sort()
Reference group: Cursor methods
What it does
Orders results by fields and directions.
Syntax
cursor.sort(sortSpec)
Use case
Show newest orders first.
Important notes
Add a unique tie-breaker for stable pagination.
sort(): practical example
db.orders.find({ customerId }).sort({ createdAt: -1, _id: -1 })💡 Explanation
Orders results by fields and directions.
When to use it
Show newest orders first.
Caution
Add a unique tie-breaker for stable pagination.
Key Takeaways
- Orders results by fields and directions.
- Typical use: Show newest orders first.