$ sort在聚合pipe道mongodb中无效$ skip

我正在使用聚合来查询用户。 每个后续请求都使用$ skip获取下一组结果,如下所示。

var aggPipeline = [ locationMatch, userToUsersMatch, usersToUserMatch, { $skip:cursor }, { $limit:perRequest } ] 

这工作正常,并返回用户,直到没有剩下的回报。 当我将$ sort添加到stream水线中时,$ skip似乎停止工作,并且在每个后续请求中都会返回重复数据(即使光标/偏移量递增)。

 aggPipeline = [ locationMatch, userToUsersMatch, usersToUserMatch, { $sort:{lastActive:-1} }, { $skip:cursor }, { $limit:perRequest } ] 

我试图通过lastActive字段sorting所有结果,然后返回它们,跳过已经返回的偏移量。 我无法理解为什么$ sort会导致重复数据在随后的调用中返回 – 它并不总是从前一个请求重复,但可以每隔一段时间附加一个用户并删除第一个用户。

任何人都知道这里发生了什么?

非常感谢。

我相信这是序列优化的结果,看一看,有和你一样的例子($ sort + $ skip + $ limit Sequence):

https://docs.mongodb.org/manual/core/aggregation-pipeline-optimization/