I have my collection like this :
{
{
"productType":"Bike",
"company":"yamaha",
"model":"y1"
},
{
"productType":"Bike",
"company":"bajaj",
"model":"b1"
},
{
"productType":"Bike",
"company":"yamaha",
"model":"y1"
},
{
"productType":"Car",
"company":"Maruti",
"model":"m1"
},
{
"productType":"Bike",
"company":"yamaha",
"model":"y2"
},
{
"productType":"Car",
"company":"Suzuki",
"model":"s1"
}
}
I want this data :
[ "Bike":{
"Yamaha":{
"y1":2,
"y2":1
},
"Bajaj":{
"b1":1
}
},
"Car":{
"Maruti":{
"m1":1
},
"Suzuki":{
"s1":1
}
]
I tried to use $replaceRoot but could not do it with that (Key should be the productType then company then the model with count of that model as the value). How to do this ? In which case specifically we should use $replaceRoot ?
Edit : since the whole answer was inside a json object, I removed that.