当$ lookup一个模式中的ID数组以从另一个模式获取详细信息时,返回空

时间:2019-05-05 04:57:48

标签: mongodb-query

这是我的ImagePost数据库结构:

{
       {
           "_id": "5cce6587ad95e78092e6efae",
           "by": "5ccdb8f3511ad70d4023f273",
           "category": "Entertainment",
           "images": [
               "5cce6583ad95e78092e6efac"
            ]
        }
    ]
}

这是我的图像数据库结构:

{
    "_id": "5cce6583ad95e78092e6efac",
    "postImage": "uploads/2019-05-05T04:24:35.015ZIMG_1570.JPG",
    "title": "Singing",
    "date": "5/4/2019",
    "category": "Entertainment",
    "by": "5ccdb8f3511ad70d4023f273",
}

我试图做:

  ImagePost.aggregate([
       {
           $match: {by: mongoose.Types.ObjectId(userId)}
       },
       {
          $project: {by: 1, category: 1, postImages: {$slice: ['$images', 3]}}
       },
       {$lookup: {
           from: 'Image',
           localField: 'images',
           foreignField: '_id',
           as: 'imageDetail'
       }}
   ])
])

几次尝试后显示的结果: 它不会显示Image Schema中图像的详细信息。

{
    "imagePosts": [
        {
            "_id": "5cce6587ad95e78092e6efae",
            "by": "5ccdb8f3511ad70d4023f273",
            "category": "Entertainment",
            "postImages": [
                "5cce6583ad95e78092e6efac"
            ],
            "imageDetail": []
        }
    ]
}

任何人都可以帮助我解决此问题,以便显示如下结果:

{
    "imagePosts": [
        {
            "_id": "5cce6587ad95e78092e6efae",
            "by": "5ccdb8f3511ad70d4023f273",
            "category": "Entertainment",
            "postImages": [
                "5cce6583ad95e78092e6efac"
            ],
            "imageDetail": [{
             "_id": "5cce6583ad95e78092e6efac",
             "postImage": "uploads/2019-05-05T04:24:35.015ZIMG_1570.JPG",
             "title": "Singing",
             "date": "5/4/2019",
             "category": "Entertainment",
             "by": "5ccdb8f3511ad70d4023f273",
            }]
        }
    ]
}

0 个答案:

没有答案