Reputation: 249
I want to apply pagination on my db. the page size is 500. now I want to sort records by page. like I want to sort records of the first page result only. is it possible?
db.getCollection('emp').find().limit(500).sort({Groupcode:1});
this will sort on whole db not on 500 records. i want to sort on pagination result.
Upvotes: 0
Views: 29
Reputation: 663
You can use an aggregate such as:
db.getCollection('emp').aggregate([
{"$limit": 500},
{"$sort": {"Groupcode": 1}}
])
This will first limit the records then apply sorting on limited records
Upvotes: 1