I have documents like:
{
"_id" : ...
"args" : {
"pos" : [ <x>, <y> ],
...
}
}
I am trying to get the min and max of the <x> and <y> values using the following aggregate pipeline. It runs but I get no values.
db.main.aggregate([
{ '$match': {
"args.pos": { '$exists': true} }
},
{ '$project':
{
'x': "$args.pos.0",
'y': "$args.pos.1"
}
},
{ '$group':
{
'_id': 'pos',
'xmin': { '$min': '$x' },
'xmax': { '$max': '$x' },
'ymin': { '$min': '$y' },
'ymax': { '$max': '$y' },
'hits': { '$sum': 1 }
}
},
{ '$project': {
'hits': '$hits',
'xmin': '$xmin',
'xmax': '$xmax',
'ymin': '$ymin',
'ymax': '$ymax',
'_id': 0 }
}
])
And I get the following output:
{
"result" : [
{
"xmin" : [ ],
"xmax" : [ ],
"ymin" : [ ],
"ymax" : [ ],
"hits" : 281
}
],
"ok" : 1
}
I have tried various different ways to acces the <x> and <y> values, but I'm new at this and obviously I'm missing something. Any help would be appreciated.
I think the issue is that I can't reach into the array. I have tried a the following simpler query with not success either:
db.main.findOne({'function':'map'},{"arguments.pos":1})
{
"_id" : ObjectId("5110407a2c8bea0f0d0000ce"),
"arguments" : {
"pos" : [
-87.90774999999735,
42.11036897863933
]
}
}
db.main.findOne({'function':'map'},{"arguments.pos.0":1})
{
"_id" : ObjectId("5110407a2c8bea0f0d0000ce"),
"arguments" : {
"pos" : [ ]
}
}
db.main.findOne({'function':'map'},{"arguments.pos[0]":1})
{ "_id" : ObjectId("5110407a2c8bea0f0d0000ce"), "arguments" : { } }
I'm running mongo shell from mongodb 2.2 if that matters.