My mongo database has two collections (samples copied from MongoDB query result):
Order:
{
"_id" : ObjectId("5f242e68ceebdd59a456fee5"),
"date": ISODate("2020-07-30T22:00:00.000Z"),
"user" : {
"$ref" : "user",
"$id" : "user_0"
},
"item": "banana"
}
User:
{
"_id" : "user_0",
"login" : "login_0",
"password" : "$2a$10$mE.qmcV0mFU5NcKh73TZx.z4ueI/.bDWbj0T1BYyqP481kGGarKLG",
"first_name" : "John",
"last_name" : "Kennedy",
"email" : "jfk@gmail.com",
"activated" : true
}
Each order belongs to a user. I put inside every Order's object a collection reference of the corresponding user.
With MongoDB's aggregation function, how would you do to get the number of Order that belongs to a user and put it inside an object alongside with his first name and his last name? I want a result to look somehow like this:
TopUser
{
"firstName": "John",
"lastName": "Kennedy",
"nbOfOrders": 3
}
Thank you very much.