Q:

mongodb aggregate multiple nested fields

I have a document structure like,
{
  "id" : 123
  "b" [
        {
         "dd"   : 21,
         "mm"   : 05,
         "yyyy" : 2015
         "prod" : "xyz"
         },
        {
         "dd"   : 22,
         "mm"   : 05,
         "yyyy" : 2015
         "prod" : "abc"
         }
       ]
 },
 {
  "id" : 512
  "b" [
        {
         "dd"   : 11,
         "mm"   : 05,
         "yyyy" : 2015
         "prod" : "xdyz"
         },
        {
         "dd"   : 22,
         "mm"   : 05,
         "yyyy" : 2015
         "prod" : "abac"
         }
       ]
 },
.....

the jsons in the list indicate date. How do I get count of dates in all documents? I want an aggregate count like
2015-05-22  count: 2
2015-05-21  count: 1
2015-05-11  count: 1

A:

This should work.   
db.Testing.aggregate([
    {'$unwind': '$b'},
    { $project: 
        { date: 
            { $concat: [
                { 
                    "$substr": [ "$b.dd", 0, 2 ] }, "-", { "$substr": [ "$b.mm", 0, 2 ] }, "-", { "$substr": [ "$b.yyyy", 0, 4 ] }
            ]}
        }
    },
    {'$group': {'_id':'$date','Count':{'$sum': 1}} }
])

Result:
{
    "result" : [ 
        {
            "_id" : "11-5-2015",
            "Count" : 1
        }, 
        {
            "_id" : "22-5-2015",
            "Count" : 2
        }, 
        {
            "_id" : "21-5-2015",
            "Count" : 1
        }
    ],
    "ok" : 1
}

