I am trying to use the $sum
in MongoDB to summarize the number of objects in an array. However it only returns 0, even when there are more objects. What am I doing wrong?
{
$lookup: {
from: "events",
let: { user: "$_id" },
pipeline: [
{
$match: {
$expr: {
$and: [
{$eq: ['$creator', '$$user']},
],
},
}
},
{
$match: {
'createdAt': {
$gte: moment(a).startOf('day').toDate(),
$lte: moment(b).startOf('day').toDate(),
}
}
},
{ $project: { _id: 0, leads: 0, bookings: 0, sales: 0 } },
],
as: "events"
}
},
{
$addFields: {
countEvents: {$sum: "$events"},
}
}