我不确定如何将以下.find
查询重新创建为mongoose查询:
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);
但是现在我需要执行一个find查询,这不是一个简单的.findOne()
。
有没有人能给我举个例子,把上面的内容变成一个mongoosedb查询?
发布于 2020-01-05 17:01:20
在mongoose docs中,它说mongoose有一个用于$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聚合。
然后你可以像这样在mongoose中使用它:
const geoNearOptions: {
... //todo
}
AdModel.aggregate([
{ $geoNear: geoNearOptions}
])
https://stackoverflow.com/questions/59601489
复制相似问题