Mongo DB duplication issue while using sorting with limit and skip in aggregation

放肆的年华 提交于 2020-01-13 04:26:33

问题


Facing an issue of duplicate records while fetching record by sorting with skip and limit:

Collection Data:

{ 
    "_id" : ObjectId("594b507c9b9469ec9da6a78b"), 
    "name" : "F", 
    "percentage" : 60.0, 
    "weightedFilter" : 2.0, 
    "like" : 1.0, 
    "attraction" : 1.0
}
{ 
    "_id" : ObjectId("594b507c9b9469ec9da6a78c"), 
    "name" : "I", 
    "percentage" : 80.0, 
    "weightedFilter" : 0.0, 
    "like" : 1.0, 
    "attraction" : 1.0
}
{ 
    "_id" : ObjectId("594b507c9b9469ec9da6a78d"), 
    "name" : "J", 
    "percentage" : 80.0, 
    "weightedFilter" : 1.0, 
    "like" : 1.0, 
    "attraction" : 1.0
}
{ 
    "_id" : ObjectId("594b507c9b9469ec9da6a78e"), 
    "name" : "A", 
    "percentage" : 100.0, 
    "weightedFilter" : 0.0, 
    "like" : 1.0, 
    "attraction" : 1.0
}
{ 
    "_id" : ObjectId("594b507c9b9469ec9da6a78f"), 
    "name" : "K", 
    "percentage" : 80.0, 
    "weightedFilter" : 1.0, 
    "like" : 1.0, 
    "attraction" : 1.0
}
{ 
    "_id" : ObjectId("594b507c9b9469ec9da6a790"), 
    "name" : "G", 
    "percentage" : 60.0, 
    "weightedFilter" : 2.0, 
    "like" : 1.0, 
    "attraction" : 1.0
}
{ 
    "_id" : ObjectId("594b507c9b9469ec9da6a791"), 
    "name" : "H", 
    "percentage" : 80.0, 
    "weightedFilter" : 0.0, 
    "like" : 1.0, 
    "attraction" : 1.0
}
{ 
    "_id" : ObjectId("594b507c9b9469ec9da6a792"), 
    "name" : "B", 
    "percentage" : 100.0, 
    "weightedFilter" : 0.0, 
    "like" : 1.0, 
    "attraction" : 1.0

}

Aggregation Query 1:

db.testing.aggregate([{$sort : { "like": -1 }},{$skip : 0},{$limit:4}]);

Output:

{ 
    "_id" : ObjectId("594b507c9b9469ec9da6a78d"), 
    "name" : "J", 
    "percentage" : 80.0, 
    "weightedFilter" : 1.0, 
    "like" : 1.0, 
    "attraction" : 1.0
}
{ 
    "_id" : ObjectId("594b507c9b9469ec9da6a78b"), 
    "name" : "F", 
    "percentage" : 60.0, 
    "weightedFilter" : 2.0, 
    "like" : 1.0, 
    "attraction" : 1.0
}
{ 
    "_id" : ObjectId("594b507c9b9469ec9da6a78c"), 
    "name" : "I", 
    "percentage" : 80.0, 
    "weightedFilter" : 0.0, 
    "like" : 1.0, 
    "attraction" : 1.0
}
{ 
    "_id" : ObjectId("594b507c9b9469ec9da6a78e"), 
    "name" : "A", 
    "percentage" : 100.0, 
    "weightedFilter" : 0.0, 
    "like" : 1.0, 
    "attraction" : 1.0
}

Aggregation Query 2:

db.testing.aggregate([{$sort : { "like": -1 }},{$skip : 4},{$limit:4}]);

{ 
    "_id" : ObjectId("594b507c9b9469ec9da6a78b"), 
    "name" : "F", 
    "percentage" : 60.0, 
    "weightedFilter" : 2.0, 
    "like" : 1.0, 
    "attraction" : 1.0
}
{ 
    "_id" : ObjectId("594b507c9b9469ec9da6a78c"), 
    "name" : "I", 
    "percentage" : 80.0, 
    "weightedFilter" : 0.0, 
    "like" : 1.0, 
    "attraction" : 1.0
}
{ 
    "_id" : ObjectId("594b507c9b9469ec9da6a78e"), 
    "name" : "A", 
    "percentage" : 100.0, 
    "weightedFilter" : 0.0, 
    "like" : 1.0, 
    "attraction" : 1.0
}
{ 
    "_id" : ObjectId("594b507c9b9469ec9da6a792"), 
    "name" : "B", 
    "percentage" : 100.0, 
    "weightedFilter" : 0.0, 
    "like" : 1.0, 
    "attraction" : 1.0
}

Conclusion:

When changing skip 0->4, got duplicate record having name F,I,A

Not getting why this happen?


回答1:


As per your collection data you are sorting by key having common values.

In first Aggregation aggregation you are using (skip,limit) => (0,4) in this case mongo is sorting the documents in order from all the documents and the result is sorted.

In second Aggregation you are again using (skip,limit) => (4,4) in this case mongo is sorting the documents from all of the document where documents can be duplicates while having same value in key.

So after sorting by your your data you should sort your data with any unique key (either ‘_id’ or ‘name’) as you wish Note : key should be unique

something like below

db.testing.aggregate([
    {
        $sort : { 
          "percentage": -1,
          "_id" : 1
        },
    },
    {
        $skip : 0
    },
    {
        $limit:4
    }
]);


来源:https://stackoverflow.com/questions/44692075/mongo-db-duplication-issue-while-using-sorting-with-limit-and-skip-in-aggregatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!