mongoose 如何在mongdb聚合中向下传递多个变量

au9on6nz  于 2023-08-06  发布在  Go
关注(0)|答案(1)|浏览(81)

我不知道如何在MongoDB聚合中向下传递特定的变量(报告id),我使用过滤器根据报告id过滤用户文档上的数组,所以我想传递报告id以仅获取此报告所拥有的对象代码片段将解释我想做的事情

const reports = await Report.aggregate([
      {
        $lookup: {
          from: 'operations',
          let: { operationId: '$operation', reportId: '$_id' /* I don't know if this is right or if there's a better way to get the report id */ },
          pipeline: [
            {
              $match: { $expr: { $eq: ['$_id', '$$operationId'] } },
            },
            {
              $lookup: {
                from: 'users',
                let: {
                  userId: '$initiator.user', type: '$initiator.type'},
                pipeline: [
                  { $match: { $expr: { $eq: ['$_id', '$$userId'] } } },
                  {
                    $project: {
                      color: '$colorCode.code',
                      avatar: 1,
                      type: '$$type',
                      clear: {
                        $filter: {
                          input: '$clear',
                          as: 'clear',
                          cond: {
                            $eq: [
                              '$$clear.report',
                              '6375856e1f7e265016ffd3c8', /* here i want to pass the reporId */
                            ],
                          },
                        },
                      },
                    },
                  },
                ],
                as: 'initiator',
              },
            },
            {
              $lookup: {
                from: 'users',
                let: {
                  userId: '$peer.user', type: '$peer.type',
                },
                pipeline: [
                  { $match: { $expr: { $eq: ['$_id', '$$userId'] } } },
                  {
                    $project: {
                      color: '$colorCode.code',
                      avatar: 1,
                      type: '$$type',
                      clear: {
                        $filter: {
                          input: '$clear',
                          as: 'clear',
                          cond: {
                            $eq: [
                              '$$clear.report',
                              '6375856e1f7e265016ffd3c8', /* same here */
                            ],
                          },
                        },
                      },
                    },
                  },
                ],
                as: 'peer',
              },
            },
            {
              $unwind: '$initiator',
            },
            {
              $unwind: '$peer',
            },
          ],

          as: 'operation',
        },
      },
      {
        $unwind: '$operation',
      },
])

字符串

y3bcpkx1

y3bcpkx11#

您可以使用$addFields阶段添加一个保存报表ID的新字段,然后在后续阶段中使用新添加的字段。

const reportId = '123someReportId'; //Replace this with the actual report id
const reports = await Report.aggregate([
  {
    $addFields: {
      reportId: '$_id', // Add a new field 'reportId' that holds the report id
    },
  },
  {
    $lookup: {
    //..... continue

字符串
然后,您可以在后续阶段中使用此reportId字段

$lookup: {
  from: 'users',
  let: {
    userId: '$initiator.user',
    type: '$initiator.type',
    reportId: '$$reportId', // Pass the report id here
  }


根据需要使用…

cond: {
  $eq: ['$clear.report', '$$reportId'], 
},

相关问题