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/.