I have a model with an address field that is type Location (KeystoneJS address + geo coordinates). As I understand it, the geo coordinates in this field are indexed as 2dSphere.
However, if I try to query with the MongooseJS near function it returns an error:
var point = [3.2642048999999815241,50.830799100000090124];
Challenge.model.find(
{status: 1}
)
.where('location')
.near({
center: point,
maxDistance: maxDistance,
spherical: true
})
.populate('waypoints')
.exec(function(err, challenges) {
if(err) return res.apiError('1000', Errors['1000']);
return res.apiResponse({
challenges: challenges
});
});
returns:
MongoError: can't find any special indices: 2d (needs index), 2dsphere (needs index), for: { status: 1, location: { $nearSphere: [ 3.264204899999982, 50.83079910000009 ], $maxDistance: 1000 } }
I don't understand why it throws an error for this.
Seems to be a bug in MongooseJS. If you use MongoDB query notation it works:
Challenge.model.find(
{
status: 1,
"location.geo": {$near: { $geometry:{ type: "Point", coordinates: location }, $maxDistance: maxDistance}}},
"-__v -updatedOn -createdOn -slug")
.populate('waypoints')
.exec(function(err, challenges) {
if(err) return res.apiError('1000', Errors['1000']);
return res.apiResponse({
challenges: challenges
});
});