Is it possible to merge array fields in while using MongoDB aggregation framework? Here is a summary problem I am trying to solve:
Sample input documents for aggregation:
{
"Category" : 1,
"Messages" : ["Msg1", "Msg2"],
"Value" : 1
},
{
"Category" : 1,
"Messages" : [],
"Value" : 10
},
{
"Category" : 1,
"Messages" : ["Msg1", "Msg3"],
"Value" : 100
},
{
"Category" : 2,
"Messages" : ["Msg4"],
"Value" : 1000
},
{
"Category" : 2,
"Messages" : ["Msg5"],
"Value" : 10000
},
{
"Category" : 3,
"Messages" : [],
"Value" : 100000
}
We want to group by 'Category' while summing up 'Value' and merging 'Messages'. I have tried this aggregation pipeline:
{group : {
_id : "$Category",
Value : { $sum : "$Value"},
Messages : {$push : "$Messages"}
}
},
{$unwind : "$Messages"},
{$unwind : "$Messages"},
{$group : {
_id : "$_id",
Value : {$first : "$Value"},
Messages : {$addToSet : "$Messages"}
}
}
The result is:
"result" : [{
"_id" : 1,
"Value" : 111,
"Messages" : ["Msg3", "Msg2", "Msg1"]
},
{
"_id" : 2,
"Value" : 11000,
"Messages" : ["Msg5", "Msg4"]
}
]
However, this completely misses Category 3 since the documents where 'Category' is 3 do not have any 'Messages' and they are dropped by the second unwind. We would like the result to include the following as well:
{
"_id" : 3,
"Value" : 100000,
"Messages" : []
}
Is there a neat way of achieving this by the aggregation framework?
preserveNullAndEmptyArrays
option to$unwind
? – tony_kpreserveNullAndEmptyArrays
should do what we were looking for. – etkarayel