node.js - Mongoose calls to geoNear with GeoJSON points as query parameters not working -


given schema defined documents containing geojson location;

 var branchschema = new schema({   location: {     'type': {       type: string,       required: true,       enum: ['point', 'linestring', 'polygon'],       default: 'point'     },     coordinates: [number]   },   name: string }); branchschema.index({location: '2dsphere'}); 

and sample data:

 [   {     "name": "a",     "location": {       "type": "point",       "coordinates": [153.027117, -27.468515 ] //brisbane, australia     }   },   {     "name": "b",     "location": {       "type": "point",       "coordinates": [153.029884, -27.45643] //also brisbane, australia     }   } ] 

the following geonear query not behaving expected. read query "given location off coast of south america, trawl through locations , find within 1 meter of provided location."

 // somewhere off east coast of south america. var point = {type: 'point', coordinates: [0.0776590, -33.7797590]};  branch.geonear(point, {maxdistance:1, spherical: true}, function (err, data) {   ...   // @ point expected data.length === 0.   // instead returning both documents.   ... }); 

what doing wrong?

  • i'm using [long,lat] when defining position per wgs84 standard.
  • runnings mongoosejs v3.8.8

the problem incorrectly using maxdistance. following expression worked.

 branch.geonear({type: "point", coordinates: [0.0776590, -33.7797590]}, {   spherical: true,    maxdistance: 1 / 6378137,    distancemultiplier: 6378137 })   .then(function (doc) {     console.log(doc);     process.exit();   });   mongoose: branches.ensureindex({ location: '2dsphere' }) { safe: undefined, background: true }   mongoose: branches.geonear(0.077659) -33.779759 { distancemultiplier: 6378137, lean: true, maxdistance: 1.567855942887398e-7, spherical: true }  []  

now query correctly discovers 2 documents in collection not within 1 meter of queried location. querying location closer home gives expected results.

 branch.geonear({type: "point", coordinates: [153.027117, -27.468515]}, {   spherical: true,    maxdistance: 1 / 6378137,    distancemultiplier: 6378137 })   .then(function (doc) {     console.log(doc);     process.exit();   });  mongoose: branches.ensureindex({ location: '2dsphere' }) { safe: undefined, background: true }   mongoose: branches.geonear(153.027117) -27.468515 { distancemultiplier: 6378137, lean: true, maxdistance: 1.567855942887398e-7, spherical: true }  [ { dis: 0.0026823704060803567,     obj:       { name: 'a',        _id: 533200e49ba06bec37c0cc22,        location: [object],        __v: 0 } } ]  

the solution?

mongodb documentation of geonear states if using geojson object maxdistance should in meters, , in radians if using coordinate pairs.

optional. distance center point. specify distance in meters geojson data , in radians legacy coordinate pairs. mongodb limits results documents fall within specified distance center point. http://docs.mongodb.org/manual/reference/command/geonear/#dbcmd.geonear

this either wrong, or understanding of wrong.

as can see above, rather specifying 1 meter maxdistance, supplied in radians.

as @ date of post geonear requires maxdistance in radians regardless of whether using geojson object or legacy coordinate pair.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -