0

I have an JS object called tasksCountMap:

const tasksCountMap = {
   'Freshman': 46,
   'Senior': 10
}

and I need get count of task for each user type in my aggregation pipe, 'Freshman', 'Senior' it's document field called gradeLevel. I'm trying do it like this:

status: {
        $let: {
          vars: {
            tasksCount: tasksCountMap['$gradeLevel'],
            completedTasksCount: '$completedTasksCount[0].count'
          },
          in: {
            $cond: {
              if: { $or: [
                { $eq: ['$$tasksCount', '$$completedTasksCount'] },
                { $lte: ['$$tasksCount', '$$completedTasksCount'] },
              ]},
              then: 'On track',
              else: 'High priority'
            }
          }
        }
      }

Also '$completedTasksCount[0].count' doesn't work to...

Can someone show right way to do this?

All 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'
            },
            {
              case: {
                $eq: ['$gradeLevel', '11']
              },
              then: 'Junior'
            },
            {
              case: {
                $eq: ['$gradeLevel', '12']
              },
              then: 'Senior'
            }
          ],
          default: "Freshman"
        }
      }
    },
    $lookup: {
      from: 'RoadmapTasksCompleted',
      let: {
        username: '$username',
        gradeLevel: '$gradeLevel'
      },
      pipeline: [{
          $match: {
            monthToComplete: {
              $in: prevMonthsNames
            },
            $expr: {
              $and: [{
                  $eq: ['$username', '$$username']
                },
                {
                  $eq: ['$gradeLevel', '$$gradeLevel']
                }
              ]
            }
          }
        },
        {
          $count: 'count'
        }
      ],
      as: 'completedTasksCount'
    },
    $project: {
      username: '$username',
      email: '$email',
      phone: '$phone',
      fullName: '$fullName',
      completedTask: { $arrayElemAt: ['$completedTasksCount', 0] },
      status: {
        $let: {
          vars: {
            tasksCount: tasksCountMap['$gradeLevel'],
            completedTasksCount: '$completedTasksCount[0].count'
          },
          in: {
            $cond: {
              if: { $or: [
                { $eq: ['$$tasksCount', '$$completedTasksCount'] },
                { $lte: ['$$tasksCount', '$$completedTasksCount'] },
              ]},
              then: '$$tasksCount',
              else:'$$tasksCount'
            }
          }
        }
      }
    }
    $limit: 10,
    $skip: 10,
  }
2
  • Can you add a sample document to the post ? What is '$completedTasksCount[0].count ? Commented Aug 16, 2018 at 13:45
  • I added a full pipeline. Commented Aug 16, 2018 at 13:56

1 Answer 1

1

You have to move the js map into the aggregation pipeline for you to be able to access the map.

I split $project stage into two and took the liberty to clean up the status calculation.

Something like

{"$project":{
  "username":"$username",
  "email":"$email",
  "phone":"$phone",
  "fullName":"$fullName",
  "completedTask":{"$arrayElemAt":["$completedTasksCount",0]},
  "tasksCountMap":[{"k":"Freshman","v":46},{"k":"Senior","v":10}]
}},
{"$addFields":{
  "status":{
    "$let":{
      "vars":{
        "tasksCount":{
          "$arrayElemAt":[
            "$tasksCountMap.v",
            {"$indexOfArray":["$tasksCountMap.k","$gradeLevel"]}
           ]
         },
        "completedTasksCount":"$completedTask.count"
      },
      "in":{
        "$cond":{
          "if":{"$lte":["$$tasksCount","$$completedTasksCount"]},
          "then":"$$tasksCount",
          "else":"$$completedTasksCount"
        }
      }
    }
  }
}}
Sign up to request clarification or add additional context in comments.

4 Comments

tasksCount variable contain a valid value, thanks) but completedTasksCount is null...
also is null on project stage... if I remove all stages below lookup i get a valid value in results completedTasksCount:[{count: 27}] but if i even just add project stage: {"$project":{"completedTask": "$completedTasksCount"}}, i get completedTasksCount:[], and don't have completedTask in result... do you know why? Thanks for help.
What framework do you use ? It looks like values are not passed from $lookup to next stage. Can you try the query in shell and verify ?
I created new question for this issue stackoverflow.com/questions/51881320/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.