How to Perform Geospatial Queries in Mongoose
I am using mongoose with NodeJS and have the following model:
var Post = new Schema({
...
location: [Number, Number],
...
});
Post.index({ location: '2d' });
Post.set('autoIndex', false);
module.exports = mongoose.model('Post', Post);
When the user makes a query for all posts, they can add an optional beside query parameter to find posts with locations within a given radius of about
GET /api/1.0/posts?near=12.3244,-1.23244
For this to work, I do the following:
if(req.query.near) {
var loc = req.query.near.split(',');
var area = { center: loc, radius: 10, unique: true, spherical: true };
if(loc.length === 2) {
query
.where('location')
.within()
.circle(area);
}
}
And then I execute the request. I did it using this mongoose documentation
I have 3 questions:
- What is the unit of radius, miles or kilometers?
- Divide radius by 3963.2 how is done here (equatorial radius of earth), or mongoose handle, what?
- What does unique mean? (top) and spherical? I suppose spherical means use mongo $ centerSphere as opposed to $ center, right?
Any help would be appreciated, especially questions 2 and 1 Thank you :)
+3
source to share
No one has answered this question yet
Check out similar questions: