我有一个数据库
[
{
"_id": 1,
"email": "amrit@gmail.com",
"status": "ACTIVE"
},
{
"_id": 2,
"email": "abc@gmail.com",
"status": "INACTIVE"
},
{
"_id": 3,
"email": "tut@gmail.com",
"status": "ACTIVE"
},
{
"_id": 4,
"email": "amrit@gmail.com",
"status": "INACTIVE"
},
{
"_id": 5,
"email": "tut@gmail.com",
"status": "ACTIVE"
},
{
"_id": 6,
"email": "cat@gmail.com",
"status": "ACTIVE"
},
]
现在我想根据状态为 ACTIVE 和 INACTIVE 的电子邮件找到该项目。我已经编写了查询来查找这样的重复项。
db.getCollection(‘employees’).aggregate([
{$group: {
_id: {email: “$email”},
uniqueIds: {$addToSet: “$_id”},
count: {$sum: 1}
}
},
{$match: {
count: {“$gt”: 1}
}
}
], {allowDiskUse:true });
这会返回 tut@gmail.com 和 amrit@gmail.com 但我只想要 amrit@gmail.com 因为它在 db 中既是 ACTIVE 又是 INACTIVE。结果应该看起来像
{
"_id": {
"email": "amrit@gmail.com"
},
"uniqueIds": [
4,
1
]
}