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} } } 
])