What you said herelast_id不要把它定义成一定是_id。它的原理是找到排序字段的最后一位,下一页从那一个开始。所以既然是根据name排序,那你的last_id其实就应该是C的name, check next time
{name: {$gt: c.name}}
But there is a problem with this paging method. The sorted fields must be unique, otherwise some documents may be skipped. For example if c.name = b.name,那么上面的查询显然会把b这条记录跳过。所以如果排序的字段不唯一,应该要加上一个第二排序字段,比如_id:
{name: 1, _id: 1}
The corresponding filter conditions should be changed to:
What you said here
last_id
不要把它定义成一定是_id
。它的原理是找到排序字段的最后一位,下一页从那一个开始。所以既然是根据name
排序,那你的last_id
其实就应该是C的name
, check next timeBut there is a problem with this paging method. The sorted fields must be unique, otherwise some documents may be skipped. For example if
c.name = b.name
,那么上面的查询显然会把b这条记录跳过。所以如果排序的字段不唯一,应该要加上一个第二排序字段,比如_id
:The corresponding filter conditions should be changed to: