I am trying to use $geoNear aggregation operator of mongoDb to calculate distances of users from current location in following way :
'$geoNear': {
near: currentLocation,
distanceField: 'distance',
spherical: true,
}
With currentLocation being something like:
{ "type" : "Point", "coordinates" : [ -122.1575745, 37.4457966 ] }
My collection is of following type (using mongoose):
users = [{
....
location : { // GeoJSON Point or I think it is ;)
type: {
type: String
},
coordinates: []
}
....
}]
I am using index (again mongoose's syntax):
userSchema.index({
location: '2dsphere'
});
Now the PROBLEM I am facing is that, if I query using currentLocation as mentioned above (in form of GeoJSON) I get weird distances (very huge numbers), but if I use currentLocation.coordinates, i.e using legacy coordinate pairs ([-122.1575745, 37.4457966]), I get correct result. But mongoDb docs for geoNear clearly says that we can query using both GeoJSON point or legacy coordinate pairs.
I am curious to know what exactly is difference between GeoJSON point and legacy coordinate pairs ?
E.g Collection:
{ "_id" : ObjectId("5277679914c6d8f00b000003"), "location" : { "type" : "Point", "coordinates" : [ 106.6202887, -6.1293536 ] } }
{ "_id" : ObjectId("5277810148219d011c000003"), "location" : { "type" : "Point", "coordinates" : [ 106.6202887, -6.1293536 ] } }
{ "_id" : ObjectId("5281c7ba2dfd7bdc64000003"), "location" : { "type" : "Point", "coordinates" : [ -86.9248483, 33.4480108 ] } }
{ "_id" : ObjectId("5281c8b82dfd7bdc64000004"), "location" : { "type" : "Point", "coordinates" : [ -74.0087126, 40.7136487 ] } }
{ "_id" : ObjectId("5281c9782dfd7bdc64000005"), "location" : { "type" : "Point", "coordinates" : [ -122.1575745, 37.4457966 ] } }
Incorrect result:
[{"location":{"type":"Point","coordinates":[-122.1575745,37.4457966]},"dis":13.69288259318155},
{"location":{"type":"Point","coordinates":[-86.9248483,33.4480108]},"dis":12697164592.388557},
{"location":{"type":"Point","coordinates":[-74.0087126,40.7136487]},"dis":16328789117.58145},
{"location":{"type":"Point","coordinates":[106.6202887,-6.1293536]},"dis":55446284682.14049},
{"location":{"type":"Point","coordinates":[106.6202887,-6.1293536]},"dis":55446284682.14049}]