shagun
shagun

Reputation: 249

MongoDB apply sort on pagination result

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

Answers (1)

Syed Kashan Ali
Syed Kashan Ali

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

Related Questions