[DOCS-2086] $geoNear in aggregation doesn't list the requirement for "spherical: true" for 2dsphere indexes Created: 11/Oct/13  Updated: 05/Aug/14  Resolved: 05/Aug/14

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: None
Fix Version/s: v1.3.9

Type: Task Priority: Major - P3
Reporter: Derick Rethans Assignee: Kay Kim (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 9 years, 28 weeks, 1 day ago

 Description   

From an email to me:

I'm trying to use the following function (take from MongoDB's website):

db.places.aggregate([
                      {
                        $geoNear: {
                                    near: [40.724, -73.997],
                                    distanceField: "dist.calculated",
                                    maxDistance: 0.008,
                                    query: { type: "public" },
                                    includeLocs: "dist.location",
                                    uniqueDocs: true,
                                    num: 5
                                  }
                      }
                   ])

I have added a 2dsphere index to my collection (stops collection):

db.stops.find().limit(1).pretty();
{
    "_id" : ObjectId("52275591f0a49d6b3b8b93ad"),
    "stopID" : 1,
    "name" : "Dulceri",
    "gps" : {
        "type" : "Point",
        "coordinates" : [
            12.5386254,
            41.8839509
        ]
    }
}

However, I have run into this problem:

db.stops.aggregate([ { $geoNear: { near: [12.492269, 41.890169], distanceField: "distance"} } ]);
Error: Printing Stack Trace
    at printStackTrace (src/mongo/shell/utils.js:37:15)
    at DBCollection.aggregate (src/mongo/shell/collection.js:897:9)
    at (shell):1:10
Thu Sep 12 18:36:24.170 aggregate failed: {
    "errmsg" : "exception: geoNear command failed: { ns: \"test.stops\", errmsg: \"exception: geoNear on 2dsphere index requires spherical\", code: 16683, ok: 0.0 }",
    "code" : 16604,
    "ok" : 0
} at src/mongo/shell/collection.js:898

Adding the

{spherical: true}

however sorts out the problem:

db.stops.aggregate([ { $geoNear: { near: [12.492269, 41.890169], distanceField: "distance", spherical: true, limit: 1 } } ]);
{
    "result" : [
        {
            "_id" : ObjectId("52275592f0a49d6b3b8b96cd"),
            "stopID" : 70744,
            "name" : "Celio Vibenna",
            "gps" : {
                "type" : "Point",
                "coordinates" : [
                    12.4928042,
                    41.8893081
                ]
            },
            "distance" : 0.000016556852802056673
        }
    ],
    "ok" : 1
}

This piece of information is missing from the
documentation? There's no mention whatsoever about having to add the
"spherical: true" option here:
http://docs.mongodb.org/manual/reference/aggregation/geoNear/.



 Comments   
Comment by Githook User [ 05/Aug/14 ]

Author:

{u'username': u'kay-kim', u'name': u'kay', u'email': u'kay.kim@10gen.com'}

Message: DOCS-2086 2.4 specific changes
Branch: v2.4
https://github.com/mongodb/docs/commit/3631d97d8a64d78ad9d6966c692720f6fdb7d862

Comment by Githook User [ 05/Aug/14 ]

Author:

{u'username': u'kay-kim', u'name': u'kay', u'email': u'kay.kim@10gen.com'}

Message: DOCS-2086 geoNear command and agg stage with 2dsphere index clarification
Branch: v2.4
https://github.com/mongodb/docs/commit/7050f2edd88015205292aed124df118dd32d0972

Comment by Githook User [ 05/Aug/14 ]

Author:

{u'username': u'kay-kim', u'name': u'kay', u'email': u'kay.kim@10gen.com'}

Message: DOCS-2086 geoNear command and agg stage with 2dsphere index clarification
Branch: master
https://github.com/mongodb/docs/commit/35d3d1862cc6a91f2ba87d12fed1a22f423b3e41

Generated at Thu Feb 08 07:42:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.