[英]Cursor based pagination in MongoDB with sorting on 2 properties
My end goal is to implement cursor based pagination in MongoDB, while sorting on 2 properties.我的最终目标是在 MongoDB 中实现基于 cursor 的分页,同时对 2 个属性进行排序。
Suppose we have the following collection:假设我们有以下集合:
+--------------------------------------+------+-------+
| _id | cost | vip |
+--------------------------------------+------+-------+
| ObjectId("5dae005e665fc34b03c70f8a") | 700 | true | <-- first query
| ObjectId("5dae005e665fc34b03c70f8c") | 50 | true | <-- first query
| ObjectId("5dae005e665fc34b03c70f88") | 1 | true | <-- second query
| ObjectId("5dae005e665fc34b03c70f8b") | 1200 | false | <-- second query
| ObjectId("5dae005e665fc34b03c70f87") | 400 | false |
| ObjectId("5dae005e665fc34b03c70f89") | 300 | false |
+--------------------------------------+------+-------+
And we want the result sorted: {vip: -1, cost: -1}
, with a limit of 2. The first query would return row 1 & 2, with the end-cursor being (ObjectId("5dae005e665fc34b03c70f8c")).我们希望结果排序: {vip: -1, cost: -1}
,限制为 2。第一个查询将返回第 1 行和第 2 行,结束光标为 (ObjectId("5dae005e665fc34b03c70f8c"))。
With this info, how do I fetch the next 2 rows (ie row 3 + 4)?有了这些信息,我如何获取接下来的 2 行(即第 3 + 4 行)?
I have tried with the following filter, but to no help.我尝试使用以下过滤器,但没有帮助。
{_id: {$lt: ObjectId('5dae005e665fc34b03c70f8c')}, cost: {$gt: 1}}
Thanks for your help.谢谢你的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.