Missing data in next aggregation stage after $lookup

拟墨画扇 提交于 2019-12-11 15:18:27

问题


thanks for come to my post. I'm new in MongoDB, and I need help with it. I need to select user data from DB, so I have a pretty simple pipeline:

const pipeline = [{
    $match: {
      type: 'student',
      counselorUserName: username
    },
    $project: {
      username: '$username',
      email: '$email',
      phone: '$phone',
      fullName: '$fullName',
      gradeLevel: {
        $switch: {
          branches: [{
              case: {
                $eq: ['$gradeLevel', '9']
              },
              then: 'Freshman'
            },
            {
              case: {
                $eq: ['$gradeLevel', '10']
              },
              then: 'Sophomore'
            },
          ],
          default: "Freshman"
        }
      }
    },
    $lookup: {
      from: 'RoadmapTasksCompleted',
      let: {
        username: '$username',
        gradeLevel: '$gradeLevel'
      },
      pipeline: [
        {
          $match: {
            monthToComplete: {
              $in: prevMonthsNames
            },
            $expr: {
              $and: [
                {
                  $eq: ['$username', '$$username']
                },
                {
                  $eq: ['$gradeLevel', '$$gradeLevel']
                }
              ]
            }
          }
        },
        {
          $group: {
            _id: null,
            count: { $sum: 1 }
          }
        }
      ],
      as: 'completedTasksCount'
    }
}

And it works fine, I get:

{
    username: "hewy",
    email: "htheodros@usfca.edu",
    phone: "4049159553",
    fullName: "Hewotte Theodros",…} 
    completedTasksCount:[{_id: null, count: 27}]
}

But I need to add new stages below lookup, and when I do that, completedTasksCount become empty array [], why it that happen and how can I fix it?

I need to add this stages:

    $project: {
      username: '$username',
      email: '$email',
      phone: '$phone',
      fullName: '$fullName',
      completedTasks: { $arrayElemAt: ['$completedTasksCount', 0] },
      tasksCountMap: tasksCountArray
    },
    $addFields: {
      status: {
        $let: {
          vars: {
            tasksCount: {
              $arrayElemAt: [
                "$tasksCountMap.v",
                {
                  $indexOfArray: ["$tasksCountMap.k", "$gradeLevel"]
                }
              ]
            },
            completedTasksCount: '$completedTasks.count'
          },
          in: {
            $cond: {
              if: { $or: [
                { $eq: ['$$tasksCount', '$$completedTasksCount'] },
                { $lte: ['$$tasksCount', '$$completedTasksCount'] },
              ]},
              then: 'On track',
              else: 'High priority'
            }
          }
        }
      }
    },

Thanks for help.

来源:https://stackoverflow.com/questions/51881320/missing-data-in-next-aggregation-stage-after-lookup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!