I am currently working on a nodejs project and I am using mongodb as my database but I have an issue. I want to make a single call using aggregation that will add the sum of a specific user order to the user collection. But I am not sure how to go about it. Please I need some suggestions/help on how to achieve this. Please see the example of objects below.
User Collection
[
{
_id: 1,
name: "Henry"
},
{
_id: 2,
name: "John"
}
]
Order Collection
[
{
_id: 1,
userId: 2
},
{
_id: 2,
userId: 2
},
{
_id: 3,
userId: 1
}
]
Expected result after making the mongoose call
[
{
_id: 1,
name: "Henry",
orderCount: 1
},
{
_id: 2,
name: "John",
orderCount: 2
}
]