我试图在MongoDB中使用$sum
来总结数组中对象的数量。但是,即使有更多对象,它也仅返回0。我究竟做错了什么?
{
$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"},
}
}