I am trying to use the $ geoNear mongoDb aggregation operator to calculate the distances of users from their current location as follows:
'$geoNear': {
near: currentLocation,
distanceField: 'distance',
spherical: true,
}
With currentLocation there will be something like:
{ "type" : "Point", "coordinates" : [ -122.1575745, 37.4457966 ] }
My collection has the following type (using mongoose):
users = [{
....
location : { // GeoJSON Point or I think it is ;)
type: {
type: String
},
coordinates: []
}
....
}]
I am using an index (again mongoose syntax):
userSchema.index({
location: '2dsphere'
});
, , , currentLocation, ( GeoJSON), ( ), currentLocation., ( [-122.1575745, 37.4457966]), . mongoDb docs geoNear , , GeoJSON, .
, GeoJSON ?
, 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 ] } }
:
[{"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}]