Array Elements in the Aggregation framework

156 views
Skip to first unread message

Christian Csar

unread,
Nov 28, 2012, 8:52:35 PM11/28/12
to mongod...@googlegroups.com
I've been looking at using the arithmetic operators in $project to calculate a flat distance to workaround the lack of geospatial queries and I've run into something kind of strange when trying to access array elements using the dot notation described here , though it seems possible based on other comments it's only meant to work with object arrays for some completely mysterious undocumented reason. I'm using the zip code dataset for testing. Apart from the dot notation appearing not to work for $project or $match despite working fine in queries, if I include loc I receive an error message, if I do not I simply get an empty array.

I'm at a bit of a loss to figure out why the dot notation for array access doesn't work since "same as queries" implies that it should. I'd very much appreciate advice on how it might be possible to make any use of the loc field within the aggregation framework as it exists today.

mongos> db.zipCollection.aggregate({"$match": {"city":"ACMAR"}}, {"$project": {city:1, pop:1, state:1, long:"$loc.0"}})
{
        "result" : [
                {
                        "city" : "ACMAR",
                        "pop" : 6055,
                        "state" : "AL",
                        "_id" : "35004",
                        "long" : [ ]
                }
        ],
        "ok" : 1
}
mongos> db.zipCollection.aggregate({"$match": {"city":"ACMAR"}}, {"$project": {city:1, pop:1, state:1, loc:1, long:"$loc.0"}})
{
        "errmsg" : "exception: the element '0' along the dotted path 'loc.0' is not an object, and cannot be navigated",
        "code" : 16014,
        "ok" : 0
}

mongos> db.zipCollection.findOne({"loc.0" : -86.51557})
{
        "city" : "ACMAR",
        "loc" : [
                -86.51557,
                33.584132
        ],
        "pop" : 6055,
        "state" : "AL",
        "_id" : "35004"
}
mongos> db.zipCollection.findOne({"$match" :{"loc.0" : -86.51557}})
null

Christian Csar

unread,
Nov 29, 2012, 8:28:39 PM11/29/12
to mongod...@googlegroups.com
It appears this is https://jira.mongodb.org/browse/SERVER-4589 which is inaccurately listed as a new feature.

Christian
Reply all
Reply to author
Forward
0 new messages