I have a collection where the documents look like this:
{
_id: \"545b9fa0dd5318a4285f7ce7\",
owner: \"admin\",
messages: [
{
id: \"100\",
You can use following aggregation :
db.collection.aggregate(
{ $match : { "_id" : ObjectId("545b9fa0dd5318a4285f7ce7") } },
{ $unwind : "$messages" },
{ $group : { "_id" : "$messages.status", "count" : { $sum : 1} } }
)
it will give you the count of status
for which message
are available, all other status
count should be consider 0.