我的mongo查询通过嵌套文档获取特定文档有什么问题?

时间:2019-11-03 03:43:48

标签: mongodb aggregation-framework

{
    "_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
}

我的查询是

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

通过此查询,我得到3个元素,

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

那不是我想要的,我想要得到的是特定的 replies _id的5dbdacd78cffef0b94580dc0。

我的期望

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

1 个答案:

答案 0 :(得分:1)

尝试$unwind之前,在回复上使用$match

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

我希望没事。