I have below users collection
[{
"_id": 1,
"adds": ["111", "222", "333", "111"]
}, {
"_id": 2,
"adds": ["555", "666", "777", "555"]
}, {
"_id": 3,
"adds": ["888", "999", "000", "888"]
}]
I need to find the duplicates inside the adds array
The expected output should be
[{
"_id": 1,
"adds": ["111"]
}, {
"_id": 2,
"adds": [ "555"]
}, {
"_id": 3,
"adds": ["888"]
}]
I have tried using many operators $setUnion, $setDifference but none of the did the trick.
Please help!!!
$unwind? An $unwind/$group/$match/$project would be a straightforward approach to compare with the answers posted so far. Also, what specific version of MongoDB server are you using?$unwindmakes some performance issue. Find duplicates inside an array is not a big deal. There should be some aggregation operator for that? Isn't it? BTW thanks for the reply.[{"111": 2, "222": 1, "333": 1}].