Finally, I was able to sort out. Mongo db has support for $facet attribute which allows you to do aggregation in multiple stages:
db.getCollection("testfixturefamilies").aggregate(
[
{
"$match" : {
"applicationType" : "Highbay"
}
},
{
"$facet" : {
"types" : [
{
"$unwind" : "$colorTempeartures"
},
{
"$group" : {
"_id" : "$colorTempeartures",
"name" : {
"$first" : "$colorTempeartures"
},
"count" : {
"$sum" : 1.0
}
}
}
],
"voltages" : [
{
"$unwind" : "$voltages"
},
{
"$group" : {
"_id" : "$voltages",
"name" : {
"$first" : "$voltages"
},
"count" : {
"$sum" : 1.0
}
}
}]
);
Advantage of using facets is that the data is just obtained once and is passed to the different stages, This is the output you will get:
{
"colorTemperatures" : [
{
"_id" : "4000",
"name" : "4000",
"count" : 1.0
}
],
"voltages" : [
{
"_id" : "120-277",
"name" : "120-277V",
"count" : 2.0
}
]
}