I have a collection called users and that has a property called orders. orders is an array if present sometimes orders might not exist in the document.
I want to take all the orders count. I did this aggregation query.
[
{
$match: {
orders: { $exists: true },
},
},
{
$project: {
ordersCount: {
$size: '$orders',
},
},
},
]
This returns each user's orders count separately. But I want to take the sum of it.(Total orders count)
How do I achieve this with MongoDB?