I have a complex group query.
Data is as follows:
Aggregation as follows:
- match by doc_id
- group by name
- project: name, name_count, amount, desc as { value: identifed by max sum of amount in that list of desc , count: sum of (percent*100)^2, percent:its percent considering amount in that list}
- same with L1 and L2. But L1 L2 are referenced field {_id, name} from another collection. So, I need to project both _id, name and what I do in point 3 above.
Therefore after execution lets say result would be :
...
},
"_id" : {
"name" : "abc"
},
"amount" : 45.0,
"count" : 4.0,
"desc" : {
"value" : "Laptop", // based on highest sum amount in group:'abc' i.e. 25.0 for laptop
"count" : 5061.72, // (56*100)^2 + (44*100)^2
"percent" : 25.0*100/45.0 = 56.0
},
...
Test Data Link: MonogoDb Playground
Udpated: 07/11/2019
Added example for calculating count
Hope I was clear. Kindly help.