【问题标题】:Mongodb aggregation lookup to add field in each array with conditionMongodb聚合查找以在每个数组中添加带有条件的字段
【发布时间】:2021-08-08 09:47:29
【问题描述】:

我有 3 个收藏。

用户:

{
   "_id":ObjectId("60a495cdd4ba8b122899d415"),
   "email":"br9@gmail.com",
   "username":"borhan"
}

面板:

{
   "_id": ObjectId("60a495cdd4ba8b122899d417"),
   "name": "borhan",
   "users": [
      {
          "role": "admin",
          "joined": "2021-05-19T04:35:47.474Z",
          "status": "active",
          "_id": ObjectId("60a495cdd4ba8b122899d418"),
          "user": ObjectId("60a495cdd4ba8b122899d415")
      },
      {
          "role": "member",
          "joined": "2021-05-19T04:35:47.474Z",
          "status": "active",
          "_id": ObjectId("60a49600d4ba8b122899d41a"),
          "user": ObjectId("60a34e167958972d7ce6f966")
       }
    ],
}

团队:

{
   "_id":ObjectId("60a495e0d4ba8b122899d419"),
   "title":"New Teams",
   "users":[
      ObjectId("60a495cdd4ba8b122899d415")
   ],
   "panel":ObjectId("60a495cdd4ba8b122899d417")
}

我想从查询 Panel colllection 中接收输出,如下所示:

{
   "_id": ObjectId("60a495cdd4ba8b122899d417"),
   "name": "borhan",
   "users": [
      {
          "role": "admin",
          "joined": "2021-05-19T04:35:47.474Z",
          "status": "active",
          "_id": ObjectId("60a495cdd4ba8b122899d418"),
          "user": ObjectId("60a495cdd4ba8b122899d415"),
          "teams":[
             {
                 "_id":ObjectId("60a495e0d4ba8b122899d419"),
                 "title":"New Teams",
                 "users":[
                      ObjectId("60a495cdd4ba8b122899d415")
                  ],
                 "panel":ObjectId("60a495cdd4ba8b122899d417")
             }
          ]
      },
      {
          "role": "member",
          "joined": "2021-05-19T04:35:47.474Z",
          "status": "active",
          "_id": ObjectId("60a49600d4ba8b122899d41a"),
          "user": ObjectId("60a34e167958972d7ce6f966")
       }
    ],
}

我的意思是我想向 Panel 集合中的每个用户添加团队字段(这是用户存在于其上的团队数组)

这是我在猫鼬中选择特定面板的匹配查询:

panel_model.aggregate([
   {
      $match: {              
         users: {
            $elemMatch: {user: ObjectId("60a495cdd4ba8b122899d415"), role:"admin"}
         }
      }
   },
])

是否可以使用 $lookup 或 $addFields 聚合获得我的输出?

【问题讨论】:

    标签: mongodb mongoose mongodb-query aggregation-framework


    【解决方案1】:

    你需要加入所有三个集合,

    • $unwind 解构数组
    • $lookup 有两种查找有助于加入集合。首先我使用Multiple-join-conditions-with--lookup,然后使用标准查找加入UsersTeams 集合。
    • $match 匹配用户id
    • $expr - 当你在查找中使用$match 时,你必须使用它。
    • $set 添加新字段
    • $group 我们已经使用 $unwind 破坏了。不,我们需要对其进行重组

    这里是代码

    db.Panel.aggregate([
      { $unwind: "$users" },
      {
        "$lookup": {
          "from": "User",
          "let": { uId: "$users.user" },
          "pipeline": [
            {
              $match: {
                $expr: {
                  $eq: [ "$_id", "$$uId" ]
                }
              }
            },
            {
              "$lookup": {
                "from": "Team",
                "localField": "_id",
                "foreignField": "users",
                "as": "teams"
              }
            }
          ],
          "as": "users.join"
        }
      },
      {
        "$set": {
          "users.getFirstElem": {
            "$arrayElemAt": [ "$users.join", 0 ]
          }
        }
      },
      {
        $set: {
          "users.teams": "$users.getFirstElem.teams",
          "users.join": "$$REMOVE",
          "users.getFirstElem": "$$REMOVE"
        }
      },
      {
        "$group": {
          "_id": "$_id",
          "name": { "$first": "name" },
          "users": { $push: "$users" }
        }
      }
    ])
    

    工作Mongo playground

    注意:希望面板和用户集合是1-1的关系。否则请告诉我

    【讨论】:

      猜你喜欢
      • 2018-07-09
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      • 2020-06-19
      • 1970-01-01
      相关资源
      最近更新 更多