4

Mongodb 聚合 排序 分页出现重复记录问题。

 2 years ago
source link: https://blog.p2hp.com/archives/8289
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Mongodb 聚合 排序 分页出现重复记录问题。

如下语句:

db.Videos.aggregate([ { $match : { $and : [{"TypeName":"C"}, {op:{$elemMatch:{"oid":1}}}, {"Properties.cateid":{$in:["f93c3508-6a97-4ee5-bb17-841603cee15a"]}}] } }, {$sort : {"CreateTime":-1} } ,{$skip:10},{$limit:10}])

当数据的CreateTime有一样的时候,翻页会出现重复记录。

看起来排序的CreateTime字段不是唯一的,因此在后续执行中顺序可能不同。要解决此问题,可以向$sort. 添加一个_id字段。由于_id总是独一无二的,它可以成为一个很好的选择。尝试:

{"$sort":{
    "CreateTime":-1,
    "_id": -1
}}



About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK