问题描述
我有以下用户收藏
[{
"_id": 1,
"adds": ["111", "222", "333", "111"]
}, {
"_id": 2,
"adds": ["555", "666", "777", "555"]
}, {
"_id": 3,
"adds": ["888", "999", "000", "888"]
}]
我需要在adds
数组中找到重复项
I need to find the duplicates inside the adds
array
预期输出应为
[{
"_id": 1,
"adds": ["111"]
}, {
"_id": 2,
"adds": [ "555"]
}, {
"_id": 3,
"adds": ["888"]
}]
我尝试使用许多运算符$setUnion
,$setDifference
,但是没有一个成功.
I have tried using many operators $setUnion
, $setDifference
but none of the did the trick.
请帮助!
推荐答案
您可以使用 $ range 生成从1
到n
的数字数组,其中n
是adds的rel =" nofollow noreferrer> $ size .然后,您可以循环"通过这些数字,并检查index
处的adds
( $ arrayElemAt )在index
之前的某个位置存在(如果是),则应将其视为重复项.您可以使用 $ indexOfArray 检查元素是否存在在数组中指定0和index
作为搜索范围.
You can use $range to generate arrays of numbers from 1
to n
where n
is the $size of adds
. Then you can "loop" through that numbers and check if adds
at index
($arrayElemAt) exists somewhere before index
if yes then it should be considered as a duplicate. You can use $indexOfArray to check if element exists in array specifying 0 and index
as search range.
然后,您只需要使用$project
和 $映射以将索引替换为实际元素.您还可以添加 $ setUnion 以避免重复的重复在最终结果集中.
Then you just need to use $project
and $map to replace indexes with actual elements. You can also add $setUnion to avoid duplicated duplicates in final result set.
db.users.aggregate([
{
$addFields: {
duplicates: {
$filter: {
input: { $range: [ 1, { $size: "$adds" } ] },
as: "index",
cond: {
$ne: [ { $indexOfArray: [ "$adds", { $arrayElemAt: [ "$adds", "$$index" ] }, 0, "$$index" ] }, -1 ]
}
}
}
}
},
{
$project: {
_id: 1,
adds: {
$setUnion: [ { $map: { input: "$duplicates", as: "d", in: { $arrayElemAt: [ "$adds", "$$d" ] } } }, [] ]
}
}
}
])
打印:
{ "_id" : 1, "adds" : [ "111" ] }
{ "_id" : 2, "adds" : [ "555" ] }
{ "_id" : 3, "adds" : [ "888" ] }
这篇关于在没有$ unwind的情况下在数组中查找重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!