Sample Mongo Document:
{
"_id": "128374",
"x": [
{
"i": "83847575",
"y": [
{
"i": "9389489283",
"t": "2014-04-11T20:46:57+0000"
},
{
"i": "9389489284",
"t": "2014-04-11T20:47:57+0000"
}
]
},
{
"i": "83847576",
"y": [
{
"i": "2382349385",
"t": "2014-01-15T23:43:29+0000"
},
{
"i": "9389489286",
"t": "2014-04-11T20:47:57+0000"
},
{
"i": "9389489286",
"t": "2014-04-11T20:49:57+0000"
}
]
}
]
}
How do you get max count of inner array 'y' per document? The problem I'm trying to solve is to get the record which has max number of 'y's. Thanks!
The following gives me the total count of 'y'.
db.coll.aggregate(
{ "$unwind" : "$x" } ,
{ "$project" : { "x" : "$x" } } ,
{ "$unwind" : "$x.y" },
{ "$group" : { _id : null, number : { $sum : 1 } } } )