match = { "$match" : { "application.packageName" : "ABC" } }
unwind = { "$unwind" : { "path" : "$application" } }
group = { "$group" : { "_id" : "$application.version" , "count" : { "$sum" : 1 } } }
pipeline = [ match , unwind , match , group ]
/* pipeline = [ unwind , match , group ]
should also work but less efficient */
db.Collection.aggregate( pipeline )
/* You will have _id rather than version in the results but a $set or
$project can take care of that. */
> [ { _id: '0.0.2', count: 1 }, { _id: '0.0.1', count: 2 } ]
You probably could use $map or $filter to avoid $unwind but I like the simplicity of $unwind. I left out the obvious $sort to get then in the order you posted.