Mongoose geo query does not return correct result Mongoose geo query does not return correct result mongoose mongoose

Mongoose geo query does not return correct result


Actually, I've been seeing some mistakes on your issue;

1- Index.

 - 2dsphere index if specifying a GeoJSON point - 2d index if specifying a point using legacy coordinates.

Your schema uses a legacy coordinate field. It's not a GeoJSON field. Because a GeoJSON has to be included a coordinate type value like the following;

location: {            'type': { type: String, default: 'Point' },             coordinates: [Number]           } 

If you want to legacy coordinate field you should use 2d index.

2- the order of lat. and lng. Your codes must start with Longitude

IMPORTANTSpecify coordinates in this order: “longitude, latitude.”

On the other hand if you wanna use legacy 2d index you might use the following codes;

{ location : { $near : [ -73.9667, 40.78 ], $maxDistance: 0.10 } }

The above codes hava a $maxDistance parameter that specifies the radius. I think you should check this out. Beacuse you must consider the following line to find 5 meters proximity.

5 meters = (5 / 6371000) radius of the earth

So, I think the following codes work;

Branch.where('location').near({    center: [long, lat],    maxDistance: 5/6371000,    spherical: true}).exec(function (err, branches) {    if (err) {        return res.status(400)            .send({                message: errors.getErrorMessage(err)            });    }    return res.json(branches);});

Or

Branch.find(    { location : { $near : [ -73.9667, 40.78 ], $maxDistance: 5/6371000 }},     function (err, branches) {        if (err) {            return res.status(400)            .send({                message: errors.getErrorMessage(err)            })        }        return res.json(branches);    })  

I hope this helps, good luck!