I am trying to $sum some values that match the next condition.
My collection:
{
_id: 1,
items: [
{
name: 'item1',
status: 'On'
},
{
name: 'item2',
status: 'On'
},
{
name: 'item3',
status: 'Off'
}]
}
I need to $sum all items that have status 'On'
This is what i am trying:
{
$addFields: {
itemsOn: { "$sum": { "$cond": [{ if: { $eq: ["$items.status", 'On'] } }, { then: 1 }, { else: 0 }] } },
}
}
But it is not working, any help? thanks in advance