I'm trying to save the longitude/latitude of a location in my user Model and query it with $geoNear
. I've looked at almost any page I could find on that topic, but still I'm not sure how to 1) create the model appropriately and how to query it.
here's the part of my model:
loc: {
//type: 'Point', // see 1
//type: String, // see 2
coordinates: [Number],
index: '2dsphere' // see 3
},
@1: According the documentation the type must be 'Point' if I want to store a point, but it throws
TypeError: Undefined type `Point` at `loc`
Did you try nesting Schemas? You can only nest using refs or arrays.
which makes sense, since I guess it should be a String
@2: this doesn't immediately throw an error, but when I try to store a user I get:
name: 'ValidationError',
errors:
{ loc:
{ [CastError: Cast to String failed for value "[object Object]" at path "loc"]
@3: if I want to create the index like that I get the error:
TypeError: Undefined type `2dsphere` at `loc.index`
Did you try nesting Schemas? You can only nest using refs or arrays.
so I tried to store the index to what felt a little bit more appropriate to me like this:
userSchema.index({'loc': '2dsphere'});
which throws the following error when I try to save a new user:
- { [MongoError: Can't extract geo keys from object, malformed geometry?:{ coordinates: [ -73.97, 40.77 ] }]
name: 'MongoError',
message: 'Can\'t extract geo keys from object, malformed geometry?:{ coordinates: [ -73.97, 40.77 ] }',
ok: 1,
n: 0,
code: 16572,
errmsg: 'Can\'t extract geo keys from object, malformed geometry?:{ coordinates: [ -73.97, 40.77 ] }',
writeConcernError:
{ code: 16572,
errmsg: 'Can\'t extract geo keys from object, malformed geometry?:{ coordinates: [ -73.97, 40.77 ] }' } }
MongoError: Can't extract geo keys from object, malformed geometry?:{ coordinates: [ -73.97, 40.77 ] }
When I updated my model on the database to to a $geoNear
command I updated my user like this:
loc: {coordinates: [-73.97, 40.77]}
and could successfully query it like this:
mongoose.model('users').db.db.command({
"geoNear": users,
"near": [
<latitude>,
<longitude>
],
"spherical": true,
"distanceMultiplier": 6378.1, // multiplier for kilometres
"maxDistance": 100 / 6378.1, // every user within 100 km
"query": { }
(I used db.command since from what I've read $geoNear
is not available when using find()
)
Everything I tried so far got me nowhere, so my question now is:
- how should my mongoose model look like?
- how can I store a user with his or her location in my database
Any help would be much appreciated!
aggregate
implementation, looks much nicer than the kind of hack I had before – Vasos