Mongo DB复制问题,同时使用带限制的排序和聚合中的跳过 [英] Mongo DB duplication issue while using sorting with limit and skip in aggregation
问题描述
在通过使用跳过和限制进行排序来获取记录时遇到重复记录的问题:
Facing an issue of duplicate records while fetching record by sorting with skip and limit:
收集数据:
{
"_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
}
汇总查询1:
db.testing.aggregate([{$sort : { "like": -1 }},{$skip : 0},{$limit:4}]);
输出:
{
"_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
}
汇总查询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
}
结论:
更改跳过0-> 4时,获得了名称为 F,I,A
When changing skip 0->4, got duplicate record having name F,I,A
不明白为什么会这样吗?
Not getting why this happen?
推荐答案
根据您的集合数据,您正在按具有共同值的键进行排序.
As per your collection data you are sorting by key having common values.
在第一个聚合聚合中,您使用(skip,limit)=>(0,4),在这种情况下,mongo从所有文档中按顺序对文档进行排序,并对结果进行排序.
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.
在第二个聚合中,您再次使用(skip,limit)=>(4,4),在这种情况下,mongo从所有文档中对文档进行排序,其中文档可以重复,而键值相同.
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.
因此,对数据进行排序后,应根据需要使用任何唯一键("_id"或名称")对数据进行排序 注意:金钥必须是唯一的
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
类似下面的
db.testing.aggregate([
{
$sort : {
"percentage": -1,
"_id" : 1
},
},
{
$skip : 0
},
{
$limit:4
}
]);
这篇关于Mongo DB复制问题,同时使用带限制的排序和聚合中的跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!