I have this schema in mongoose:
new Schema({
location: {
type: {
type: String,
enum: ['Point']
},
coordinates: {
type: [Number],
index: '2dsphere'
}
}
})
I want to sort base on distance from supplied lng and lat. I'm just learning mongoose so I ran into an issue I'm getting this error when trying to sort:
unable to find index for $geoNear query
This is what I've tried (in this case lat and lng are 1):
const banners = await FullBanner
.where('location')
.near({
center: {
type: 'Point',
coordinates: [1, 1]
},
maxDistance: 10 * 1000
})
.exec();
const banners = await FullBanner
.where('location')
.near({ center: [1, 1] });
const banners = await FullBanner
.find({})
.where('location')
.near({ center: [1, 1] });
const banners = await FullBanner.find({
location: {
$near: {
$geometry:{
type: "Point",
coordinates: [1, 1]
}
}
}
});
This is like in the example at their official website: https://mongoosejs.com/docs/api.html#query_Query-near Can someone explain what I'm doing wrong and what is a proper way to sort my schema based on distance