I am using $geoNear
and near
inside the aggregate in MongoDB. I have hosted my MongoDB database into mlabs. And everything working fine on my local, But don't know why when I deploy my app to live and I am getting the below error:
"geoNear command failed: { ok: 0.0, errmsg: \"more than one 2dsphere index, not sure which to run geoNear on
Below is the code which I have used:
Shops.aggregate([
{
$geoNear: {
near: {
type: "Point",
coordinates: coordinates
},
distanceField: "dist.calculated",
maxDistance: 80467,
spherical: true
}
}
])
.then((products)=>{
res.json(products);
})
Can anyone please help me with the same?
As the error message indicated, this is because you have more than one 2dsphere
indexes, so $geoNear
doesn't know which one to use.
In this situation, you can either:
key
parameter as mentioned in the $geoNear documentation:If your collection has multiple 2d and/or multiple 2dsphere indexes, you must use the key option to specify the indexed field path to use. Specify Which Geospatial Index to Use provides a full example.
The error is mentioned in the docs as well:
If there is more than one 2d index or more than one 2dsphere index and you do not specify a key, MongoDB will return an error.
You can use db.collection.getIndexes() to list all indexes defined on the collection.
Here's an example of using the key
parameter:
> db.test.insert([
{_id:0, loc1:{type:'Point',coordinates:[1,1]}, loc2:{type:'Point',coordinates:[2,2]}},
{_id:1, loc1:{type:'Point',coordinates:[2,2]}, loc2:{type:'Point',coordinates:[1,1]}}
])
Then I create two 2dsphere
indexes:
> db.test.createIndex({loc1:'2dsphere'})
> db.test.createIndex({loc2:'2dsphere'})
Running $geoNear
without specifying key
will output the error:
> db.test.aggregate({$geoNear:{near:{type:'Point',coordinates:[0,0]},distanceField:'d'}})
...
"errmsg": "more than one 2dsphere index, not sure which to run geoNear on",
...
Using key: loc1
will sort the result according to the loc1
index (_id: 0
comes before _id: 1
):
> db.test.aggregate(
{$geoNear: {
near: {type: 'Point',coordinates: [0,0]},
distanceField: 'd',
key: 'loc1'}})
{ "_id": 0, "loc1": { "type": "Point", "coordinates": [ 1, 1 ] }, "loc2": { "type": "Point", "coordinates": [ 2, 2 ] }, "d": 157424.6238723255 }
{ "_id": 1, "loc1": { "type": "Point", "coordinates": [ 2, 2 ] }, "loc2": { "type": "Point", "coordinates": [ 1, 1 ] }, "d": 314825.2636028646 }
And, using key: loc2
will sort the result according to the loc2
index (_id: 1
comes before _id: 0
):
> db.test.aggregate(
{$geoNear: {
near: {type: 'Point',coordinates: [0,0]},
distanceField: 'd',
key: 'loc2'}})
{ "_id": 1, "loc1": { "type": "Point", "coordinates": [ 2, 2 ] }, "loc2": { "type": "Point", "coordinates": [ 1, 1 ] }, "d": 157424.6238723255 }
{ "_id": 0, "loc1": { "type": "Point", "coordinates": [ 1, 1 ] }, "loc2": { "type": "Point", "coordinates": [ 2, 2 ] }, "d": 314825.2636028646 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With