I have mongo query which does the group operation on the documents.
I have almost got the expected results except that I want to refine the results without empty or
You need an extra $match pipeline step that will filter the incoming documents based on the embedded field "$productAttribute.colour" existing and not null:
db.productMetadata.aggregate([
{
"$match": {
"productAttribute.colour": {
"$exists": true,
"$ne": null
}
}
},
{
"$group": {
"_id": {
"color": "$productAttribute.colour",
"gender": "$productAttribute.gender"
},
"count": {
"$sum": 1
}
}
}
]);