简体   繁体   English

MongoDB对分页结果应用排序

[英]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. 页面大小为500。现在我要按页面对记录进行排序。 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. 这将对整个数据库而不是500条记录进行排序。 i want to sort on pagination result. 我想对分页结果进行排序。

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 这将首先限制记录,然后对有限的记录进行排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM