问题
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