mongodb - Mongo geolocation using $near and 2d index not being accurate -


i've written application finds local establishments , delivers them via restful api. stack is: express, express-resource , mongoose. here sample of model:

var placeschema = new mongoose.schema(     {         name: {             type: string,             required: true         },         geolocation: {             lat: number,             lng: number         },         address: {             type: string         }     } ); placeschema.index({     geolocation: '2d' }); 

i've checked few times , lon/lat values being correctly stored in db, there aren't data errors.

then run query grab data specific query values:

mongoose.connection.db.executedbcommand(     {         geonear: 'places',         near: [             parsefloat(req.body.lat),             parsefloat(req.body.lng)         ],         num: limit,         query: {             // additional queries             // don't impact geo results.         },         spherical: true,         distancemultiplier: 6371, // converting results km         maxdistance: distance / 6371,     },     function(err, result)     {         res.send(200, {             status: true,             data:   theaters         });     } ); 

there few issues results it's returning: a) calculation in km really wrong. in cases there's 6-7km difference, varies, b) places closer appearing farther other places.

i'm using direct mongoose query because return calculated distance (which require api returns). switching mongoose find method apparently wouldn't let me gain access data.

wondering if index perhaps wrong? should 2dsphere instead? documentation in regard confusing, of examples see use 2d.

thanks bunch!

no matter type of geospatial indexing use in mongodb, must store longitude first , latitude.

from http://docs.mongodb.org/manual/core/2d/#store-points-on-a-2d-plane , multiple other places in docs:

whether array or document, if use longitude , latitude, store coordinates in order: longitude, latitude.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -