Mongodb 检查具有内部数组的文档数组中某些字段的重复项
我有两个物体Mongodb 检查具有内部数组的文档数组中某些字段的重复项,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我有两个物体 { _id: ObjectId("5cd9010310b80b3e38cd3f88") subGroup: [ bookList: [ { title: "A good book", id: "abc123" } ] ] } { _id: ObjectId("5cd9010710b80b3e3
{
_id: ObjectId("5cd9010310b80b3e38cd3f88")
subGroup: [
bookList: [
{
title: "A good book",
id: "abc123"
}
]
]
}
{
_id: ObjectId("5cd9010710b80b3e38cd3f89")
subGroup: [
bookList: [
{
title: "A good book",
id: "abc123"
}
]
这是两个不同的对象。我想检测标题重复的这两个对象的出现(例如相同)
我试过这个问题
db.scope.aggregate({"$unwind": "$subGroup.bookList"}, {"$group" : { "_id": "$title", "count": { "$sum": 1 } } }, {"$match": {"id" :{ "$ne" : null } , "count" : {"$gt": 1} } })
我查看了stackoverflow上的其他线程。然而,它并没有给我任何回报。如何解决这个问题?这里有几个问题:
应分别在$unwind
子组和
子组上运行
- 为
阶段指定$group
时,应使用完整路径(\u id
)子组.bookList.title
- 在
阶段中,您要检查$match
(而不是\u id
)是否为id
$ne
null
db.col.aggregate([
{"$unwind": "$subGroup"},
{"$unwind": "$subGroup.bookList"},
{"$group" : { "_id": "$subGroup.bookList.title", "count": { "$sum": 1 } } },
{"$match": { "_id" :{ "$ne" : null } , "count" : { "$gt": 1} } }
])