Mongodb 我的mongo查询获取特定的by nest文档有什么问题?

Mongodb 我的mongo查询获取特定的by nest文档有什么问题?,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我的问题是 { "_id" : ObjectId("5dbdacc28cffef0b94580dbd"), "owner" : { "image" : "https://lh3.googleusercontent.com/a-/AAuE7mCpG2jzbEdffPgdeVWnkBKwyzCCwEB1HMbU1LAVAg=s50", "fullname" : "soeng kanel", "userID" : "5da85558886

我的问题是

{
    "_id" : ObjectId("5dbdacc28cffef0b94580dbd"),
    "owner" : {
        "image" : "https://lh3.googleusercontent.com/a-/AAuE7mCpG2jzbEdffPgdeVWnkBKwyzCCwEB1HMbU1LAVAg=s50",
        "fullname" : "soeng kanel",
        "userID" : "5da85558886aee13e4e7f044"
    },
    "image" : "soeng kanel-1572711618984.png",
    "body" : "sdadadasdsadadas sds",
    "date" : ISODate("2019-11-02T16:20:05.558Z"),
    "comments" : [ 
        {
            "user" : "5da85558886aee13e4e7f044",
            "fullname" : "soeng kanel",
            "username" : "",
            "comment" : "sdsfdsfdsfds",
            "_id" : ObjectId("5dbdacc78cffef0b94580dbf"),
            "replies" : [ 
                {
                    "likes" : [ 
                        "5da85558886aee13e4e7f044"
                    ],
                    "date" : ISODate("2019-11-02T16:20:05.558Z"),
                    "_id" : ObjectId("5dbdacd78cffef0b94580dc0"),
                    "reply" : "r1111111",
                    "username" : "",
                    "fullname" : "soeng kanel",
                    "user" : "5da85558886aee13e4e7f044"
                }, 
                {
                    "likes" : [],
                    "date" : ISODate("2019-11-02T16:20:05.558Z"),
                    "_id" : ObjectId("5dbdacdb8cffef0b94580dc1"),
                    "reply" : "r222222",
                    "username" : "",
                    "fullname" : "soeng kanel",
                    "user" : "5da85558886aee13e4e7f044"
                }, 
                {
                    "likes" : [],
                    "date" : ISODate("2019-11-03T03:04:23.528Z"),
                    "_id" : ObjectId("5dbe4749fa751f05afcc1bd6"),
                    "reply" : "33333333",
                    "username" : "",
                    "fullname" : "soeng kanel",
                    "user" : "5da85558886aee13e4e7f044"
                }
            ],
            "date" : ISODate("2019-11-02T16:20:05.558Z"),
            "likes" : []
        }
    ],
    "likes" : [ 
        "5da85558886aee13e4e7f044"
    ],
    "project" : {},
    "__v" : 2
}
通过这个查询,我得到了3个元素

db.getCollection("posts").aggregate([
    { $match: {_id: ObjectId("5dbdacc28cffef0b94580dbd"), "comments._id": ObjectId("5dbdacc78cffef0b94580dbf") }},
    { $unwind: "$comments"},
    { $match: { "comments._id": ObjectId("5dbdacc78cffef0b94580dbf")}},

    { $project: {"replies": "$comments.replies", _id: 0}},

    { $match: { "replies._id": ObjectId("5dbdacd78cffef0b94580dc0")}},

    { $project: {"likes": "$replies.likes", _id: 0}},
    ])
这不是我想要的,我想要的是通过特定的方式获得 此_id5dbdacd78cffef0b94580dc0的回复

还有我的期望

{
    "likes" : [ 
        [ 
            "5da85558886aee13e4e7f044"
        ], 
        [], 
        []
    ]
}
在对答复使用$match stage之前,请尝试对答复使用$unwind

{
        "likes" : [ 
            [ 
                "5da85558886aee13e4e7f044"
            ]
        ]
    }
上述查询以以下方式生成输出:

db.collection.aggregate([
  {
    $match: {
      _id: ObjectId("5dbdacc28cffef0b94580dbd"),
      "comments._id": ObjectId("5dbdacc78cffef0b94580dbf")
    }
  },
  {
    $unwind: {
      path: "$comments",
      preserveNullAndEmptyArrays: false
    }
  },
  {
    $match: {
      "comments._id": ObjectId("5dbdacc78cffef0b94580dbf")
    }
  },
  {
    $project: {
      "replies": "$comments.replies",
      _id: 0
    }
  },
  {
    $unwind: {
      path: "$replies",
      preserveNullAndEmptyArrays: false
    }
  },
  {
    $match: {
      "replies._id": ObjectId("5dbdacd78cffef0b94580dc0")
    }
  },
  {
    $project: {
      "likes": "$replies.likes"
    }
  }
])
我希望没问题

[
  {
    "likes": [
      "5da85558886aee13e4e7f044"
    ]
  }
]