I joined with other two collections and want to group this by category.
And I want to project like this:
[
{
"_id": "101",
"cars": [
{
"carName": "CyberTruck",
"driverName": "Tom",
"views": 50
},
{
"carName": "Model 3",
"driverName": "Tom",
"views": 40
},
{
"carName": "Model X",
"driverName": "Tom",
"views": 30
}
],
"categoryName": "Tesla"
},
{
"_id": "102",
"cars": [
{
"carName": "X1",
"driverName": "Mary",
"views": 20
}
],
"categoryName": "BMW"
}
]
In addition, I want to sort by views from biggest to smallest and project the only first 3 elements for each category.
and this is what I tried before: https://mongoplayground.net/p/FPCU4aV9a9X
However, I don't know how to join with driver collection to match driver name with driverId in it.