node.js - geoNear() is not working as expected when using mongoose -
i'm using mongodb , mongoose odm app. have document holding location restaurant. model (schema) this:
var locationschema = new mongoose.schema({ name: {type:string, required:true}, address: string, coords: {type:[number], index:'2dsphere', required:true}, });
and here sample data:
"_id" : objectid("56b39d9673ff055a098dee71"), "name" : "holycow steakhouse", "address" : "somewhere", "coords" : [ 106.7999044, -6.2916982 ]
then use mongoose restaurant location somewhere within approximately 2 km restaurant. read mongodb doc have supply maxdistance param in radiance , distancemultiplier earth radius, put following code controller:
var point = { type: "point", coordinates: [106.8047355, -6.2875187] // test data, approximately 2 km restaurant } var geooptions = { spherical: true, num: 10, maxdistance: 5 / 6371 , // set maximum distance 5 km. make sure found place. distancemultiplier: 6371 } loc.geonear(point, geooptions, function(err, results, stats){ var locations = []; if(err){ console.log(err); sendjsonresponse(res, 404, err); } else { results.foreach(function(doc){ locations.push({ distance: doc.dis, name: doc.obj.name, address: doc.obj.address, _id: doc.obj._id }); }); sendjsonresponse(res, 200, locations); } });
but failed find restaurant. have read docs 2 hours, still have no clue. what's wrong code?
try this:
var geooptions = { spherical: true, num: 10, maxdistance: 5 * 1000 , // convert kilometers meters }
i think reading same book. code familiar, , encountered same problem.
your code not working because assume maxdistace
using unit radians. no, naxdistance
using meters. need remove distancemultiplier
because convert maxdistance
radians, not right unit.
try link: mongodb docs $maxdistance
Comments
Post a Comment