Mongoose聚合检查对象的两次嵌套数组(如果存在特定值)并返回父ID

yvt65v4c  于 2023-04-12  发布在  Go
关注(0)|答案(1)|浏览(104)

我在mongoose中有一个聚合管道,它可以获取帖子以及喜欢和投票在返回查询结果之前,我在聚合中检查用户是否(id)是否喜欢该帖子,我在第02节的操作中使用$处理。然而,我在执行同样的检查用户是否投票时遇到了问题,因为我在这里面对一个嵌套的对象数组。我的方法是嵌套$in操作,但这不起作用(在03之后)。

return Post.aggregate([
    {"$match": { "issuer.companyId": new mongoose.Types.ObjectId(startupId) } },
    {"$sort": { "createdAt": -1 } },
    //02
    {"$lookup":{
            "from":"postlikes",
            "localField":"_id",
            "foreignField":"_id",
            "as":"likes"
        }},
    {"$addFields": { "likes": {"$first": "$likes.likes._id"}, } },
    {"$addFields": { "liked": {"$in": [ new mongoose.Types.ObjectId(userId), "$likes" ] } } },
    {"$unset": "likes"},
    //03
    {"$lookup":{
            "from":"polls",
            "localField":"_id",
            "foreignField":"_id",
            "as":"polls"
        }},
    {"$addFields": { "polls": {"$first": "$polls.options"}, } },
    //todo: Check if the user id is included in one of the vote arrays and return the selected option id  
])

当前输出如下所示:

[
     {
         "_id": "6414698183b285f6f16a17be",
         "poll": [
             {
                 "text": "Facebook",
                 "votes": 0,
                 "_id": "6414698183b285f6f16a17c0"
             },
             {
                 "text": "TikTok",
                 "votes": 2,
                 "_id": "6414698183b285f6f16a17c1"
             }
         ],
         "liked": true,
         "polls": [
             {
                 "_id": "6414698183b285f6f16a17c0", 
                 "voters": []
             },
             {
                 "_id": "6414698183b285f6f16a17c1", //the poll option which should be added in pipeline as new field 
                 "voters": [
                     {
                         "name": “XX”,
                         "timestamp": 1679218784,
                         "_id": "63d3892fd1cc6b5711c67799" //the user
                     }
                 ]
             }
         ]
     },
]

假设用户(63 d3892 fd 1cc 6 b5711 c67799)投票支持最后一个选项(6414698183 b285 f6 f16 a17 c1),我想创建一个新字段($addField),其中包含所选投票选项的ID。
预期结果如下所示

[
     {
         "_id": "6414698183b285f6f16a17be",
         "poll": [
             {
                 "text": "Facebook",
                 "votes": 0,
                 "_id": "6414698183b285f6f16a17c0"
             },
             {
                 "text": "TikTok",
                 "votes": 2,
                 "_id": "6414698183b285f6f16a17c1"
             }
         ],
         "liked": true,
         "pollVote”: 6414698183b285f6f16a17c1 //This user voted for option TikTok 
     },
]

如果这不起作用,我将不得不在控制器中处理它。
干杯

628mspwn

628mspwn1#

一个选项是使用$filter添加一个步骤:

db.collection.aggregate([
  {$project: {
      poll: 1,
      liked: 1,
      pollVote: {
        $getField: {
          input: {
            $first: {
              $filter: {
                input: "$polls",
                cond: {
                  $in: [
                    userId,
                    "$$this.voters._id"
                  ]
                }
              }
            }
          },
          field: "_id"
        }
      }
  }}
])

了解它在playground example上的工作原理

相关问题