我不确定如何将以下.find
查询重新创建为猫鼬查询:
db.places.find({
loc: { $geoWithin: { $centerSphere: [[-74, 40.74], 100 / 3963.2] } }
});
我已经制定了方案
const AdScheme = new Schema({
location: {
coordinates: [{ 0: Number, 1: Number }]
},
type: String,
name: String
});
export default mongoose.model("campaign", AdScheme);
但是现在我需要进行查找查询,这不是简单的.findOne()
。
任何人都可以举一个将以上内容转换为mongoosedb查询的例子吗?
答案 0 :(得分:1)
在猫鼬docs中,猫鼬有$geoWithin的辅助方法。
首先,必须像这样更新架构:
const AdScheme = new Schema({
location: {
type: {
type: String,
default: 'Point',
enum: ['Point']
},
coordinates: [Number]
},
type: String,
name: String
});
然后像这样查询:
AdModel.find().where('loc')
.within({ center: [50,50], radius: 10, unique: true, spherical: true })
另一种选择是使用mongoDB $geoNear聚合。
然后您可以像这样在猫鼬中使用它:
const geoNearOptions: {
... //todo
}
AdModel.aggregate([
{ $geoNear: geoNearOptions}
])