mongoose: Unable to perform geospatial queries
When trying to issue a geospatial query using ‘near’ you get back an error “Can’t use $near”. I have also tried with geoNear and nearSphere as well.
Example:
var Schema = new mongoose.Schema({
placeName: String,
geo: {
type: { type: String, enum: "Point", default: "Point" },
coordinates: { type: [ Number ], default: [ 0,0 ] }
}
});
somemodel.where('geo').near({ center: [50, 50] }).exec(function(err, locs){
console.log(locs);
})
The above will return: [Error: Can’t use $near]
I have also tried the following:
model.find({
geo: {
$near: {
$geometry: {
type: "Point",
coordinates: [50, 50]
},
$maxDistance: maxDistance
}
}
}, function(err, locs) {
console.log(locs);
});
It will also result in the same error: [Error: Can’t use $near]
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 2
- Comments: 17
Commits related to this issue
- refactor(schema): move array geospatial query casting into separate file Re: #4014 — committed to Automattic/mongoose by vkarpov15 8 years ago
- test(schema): repro #4014 — committed to Automattic/mongoose by vkarpov15 8 years ago
- test(document): fix near sphere test for #4014 — committed to Fonger/mongoose by Fonger 6 years ago
- test(document): fix near sphere test for #4014 — committed to Fonger/mongoose by Fonger 6 years ago
Looks like this was just an instance of a missing query operator for single embedded subdocs, much like #4044. Will be fixed when 4.4.14 is released.