I'm a beginner to Mongodb, I want to execute the following query to find the total number of assignments for each employee with assignment progress "working".
db.employees.aggregate([
{
"$lookup": {
"from": "assignment",
"localField": "empno",
"foreignField": "emp_no",
"as": "assignments"
}
},
{
$project: {
points: {
$sum: {
$map: {
input: "$assignments",
in: { $match:{$Sthis.progress:"Working"}}
}
}
},
empno: 1
}
}
])
When I execute this query I get the error "Invalid $project:: caused by:: Unrecognized expression '$match'". How can I solve this?
CodePudding user response:
add filter to input of map and set condition in filter
db.employees.aggregate([
{
"$lookup": {
"from": "assignment",
"localField": "empno",
"foreignField": "emp_no",
"as": "assignments"
}
},
{
$project: {
points: {
$sum: {
$map: {
input:{"$filter":
{ "input": "$assignments", "as": "z", "cond": {$eq:["$$z.progress","Working"]} } },
as :"s",
in: "$$s"
}
}
},
empno: 1
}
}
])