node.js - Create & Find GeoLocation in mongoose -
i'm trying save longitude/latitude of location in user model , query $geonear
. i've looked @ page find on topic, still i'm not sure how 1) create model appropriately , how query it.
here's part of model:
loc: { //type: 'point', // see 1 //type: string, // see 2 coordinates: [number], index: '2dsphere' // see 3 },
@1: according documentation type must 'point' if want store point, throws
typeerror: undefined type `point` @ `loc` did try nesting schemas? can nest using refs or arrays.
which makes sense, since guess should string
@2: doesn't throw error, when try store user get:
name: 'validationerror', errors: { loc: { [casterror: cast string failed value "[object object]" @ path "loc"]
@3: if want create index error:
typeerror: undefined type `2dsphere` @ `loc.index` did try nesting schemas? can nest using refs or arrays.
so tried store index felt little bit more appropriate me this:
userschema.index({'loc': '2dsphere'});
which throws following error when try save new user:
- { [mongoerror: can't extract geo keys object, malformed geometry?:{ coordinates: [ -73.97, 40.77 ] }] name: 'mongoerror', message: 'can\'t extract geo keys object, malformed geometry?:{ coordinates: [ -73.97, 40.77 ] }', ok: 1, n: 0, code: 16572, errmsg: 'can\'t extract geo keys object, malformed geometry?:{ coordinates: [ -73.97, 40.77 ] }', writeconcernerror: { code: 16572, errmsg: 'can\'t extract geo keys object, malformed geometry?:{ coordinates: [ -73.97, 40.77 ] }' } } mongoerror: can't extract geo keys object, malformed geometry?:{ coordinates: [ -73.97, 40.77 ] }
when updated model on database to $geonear
command updated user this:
loc: {coordinates: [-73.97, 40.77]}
and query this:
mongoose.model('users').db.db.command({ "geonear": users, "near": [ <latitude>, <longitude> ], "spherical": true, "distancemultiplier": 6378.1, // multiplier kilometres "maxdistance": 100 / 6378.1, // every user within 100 km "query": { }
(i used db.command since i've read $geonear
not available when using find()
)
everything tried far got me nowhere, question is:
- how should mongoose model like?
- how can store user or location in database
any appreciated!
if want schema support geojson, first need construct properly:
var userschema = new schema({ loc: { type: { type: string }, coordinates: [number], } });
that makes sure there no confusion "type" keyword of schema defintition. if want support whole range of geojson types, can make bit looser:
var userschema = new schema({ loc: { type: { type: string }, coordinates: [] } });
next want tie index shema:
userschema.index({ "loc": "2dsphere" });
then of course define model , store things correctly:
var user = mongoose.model( "user", userschema ); var user = new user({ "loc": { "type": "point", "coordinates": [-73.97, 40.77] } });
noting data must in longitude latitude order supported geojson , mongodb geospatial query forms.
next, rather dig obscure usages of database commands directly on raw driver method, use things instead directly supported, , better. such $geonear
.aggregate()
method:
user.aggregate( [ { "$geonear": { "near": { "type": "point", "coordinates": [<long>,<lat>] }, "distancefield": "distance", "spherical": true, "maxdistance": 10000 }} ], function(err,results) { } )
and because data geojson, distances converted meters there no need other conversion work.
also note have been messing around this, unless drop collection index tried still there , cause problems.
you can drop indexes collection in mongodb shell ease:
db.users.dropindexes();
or since need data re-shaping, drop collection , start again:
db.users.drop();
set things , have no problems.
ReplyDeleteHi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer Node js Training in Chennai . learn from or Javascript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai
ReplyDeleteHi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer Node js Training in Chennai . learn from or Javascript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai