【问题标题】:How to populate a field in MongoDB aggregate $lookup result?如何在 MongoDB 聚合 $lookup 结果中填充字段?
【发布时间】:2020-08-19 02:42:06
【问题描述】:

我无法使用互联网上的其他答案或我的尝试找到解决方案。这就是我创建一个新问题的原因。

我有 2 个集合 - postsusers,我使用 MongoDB + Mongoose。

如何使用聚合来拥有属于某个帖子的所有用户?

我需要类似 populate 的东西,但我需要使用聚合(我使用更复杂的聚合,这只是一个示例)

我有字段 likes,其中包含所有喜欢某个帖子的 用户。所以 likes.users.user 持有一个喜欢的用户的 ID。

如何通过聚合更改此字段以保存具有用户信息的对象,而不仅仅是一个 ID?请参阅所需的输出

聚合输出之前:

{
    "_id": "5eb011af5c9246204944156e",
    "title": "My awesome blog post",
    "likes": {
        "count": 3,
        "users": [
            {
                "_id": "5eb011ba5c92462049441570",
                "user": "5e915d5e116d010560470107"
            },
            {
                "_id": "5eb012535c92462049441576",
                "user": "5e915d76116d010560470108"
            },
            {
                "_id": "5eb018d57ae41a2171478ff1",
                "user": "5e915d8b116d010560470109"
            }
        ]
    }
}

期望的输出:

{
    "_id": "5eb011af5c9246204944156e",
    "title": "My awesome blog post",
    "likes": {
        "count": 3,
        "users": [
            {
                "_id": "5eb011ba5c92462049441570",
                "user": {
                    "_id": "5e915d5e116d010560470107",
                    "name": "John Doe",
                    "role": "member"
                } 
            }, 
            {
                "_id": "5eb011ba5c92462049441576",
                "user": {
                    "_id": "5e915d5e116d010560470108",
                    "name": "Daniel Grant",
                    "role": "member"
                } 
            },
            {
                "_id": "5eb018d57ae41a2171478ff1",
                "user": {
                    "_id": "5e915d5e116d010560470109",
                    "name": "Tiffany Holland",
                    "role": "administrator"
                } 
            }
        ]
    }
}

当前错误输出:

{
    "_id": "5eb011af5c9246204944156e",
    "title": "My awesome blog post",
    "likes": {
        "count": 3,
        "users": [
            {
                "_id": "5eb011ba5c92462049441570",
                "user": {
                    "_id": "5e915d5e116d010560470107",
                    "name": "John Doe",
                    "role": "member"
                } 
            }, 
            {
                "_id": "5eb011ba5c92462049441576",
                "user": {
                    "_id": "5e915d5e116d010560470107",
                    "name": "John Doe",
                    "role": "member"
                } 
            },
            {
                "_id": "5eb018d57ae41a2171478ff1",
                "user": {
                    "_id": "5e915d5e116d010560470107",
                    "name": "John Doe",
                    "role": "member"
                } 
            }
        ]
    }
}

这基本上是我需要的,但它只是 3 的一个用户,它显示重复。为什么?

我尝试过的汇总:

const aggregatedData = await Post.aggregate([
    { $match: { _id: mongoose.Types.ObjectId( id ) } },
    {
        $lookup: {
            from: "users",
            localField: "likes.users.user",
            foreignField: "_id",
            as: "likesUsers"
        }
    },
    {
        $unwind: "$likesUsers"
    },
    {
        $addFields: {
            "likes.users.user": "$likesUsers",
        }
    },
    {
        $project: {
            "likesUsers": 0,
        }
    }
]);

谢谢

【问题讨论】:

    标签: mongodb mongoose aggregation-framework


    【解决方案1】:

    这个聚合会得到想要的结果。

    db.posts.aggregate([
      {
          $lookup: {
              from: "users",
              localField: "likes.users.user",
              foreignField: "_id",
              as: "likesUsers"
          }
      },
      { 
          $addFields: {
              "likes.users": {
                  $map: {
                       input: "$likes.users", as: "usr",
                       in: {
                           user: {
                               $arrayElemAt: [ 
                                   { $filter: {
                                         input: "$likesUsers", as: "likeUsr",
                                         cond: {
                                             $eq: [ "$$usr.user", "$$likeUsr._id" ]
                                         }
                                    }
                                }, 0 ]
                           },
                           _id: "$$usr._id"
                       } 
                   }
               }
          }
      },
      {
          $project: { likesUsers: 0 }
      }
    ] )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-25
      • 2021-02-16
      • 2018-08-06
      • 2022-01-05
      • 2021-10-22
      • 1970-01-01
      • 2020-07-30
      • 2018-07-21
      相关资源
      最近更新 更多