【发布时间】:2021-07-22 14:30:27
【问题描述】:
我有一个包含此属性的架构(Tour)。 位置是 GeoJSON Point 类型。
location: {
type: {
type: String,
enum: ['Point'],
required: true
},
coordinates: {
type: [Number],
required: true
},
index: '2dsphere'
},
用于创建游览的控制器如下所示
const createTour = async (req, res) => {
var newTour = new TourDB.Tour({
...
location: { type: 'Point', coordinates: [req.body.tour.loc[0], req.body.tour.loc[1]] },
...
})
newTour.save(async (err, doc) => {
if (!err) {
return res.status(200).send(doc)
} else {
return res.status(500).send({ errorMessage: err })
}
})
在前端,我将使用此属性创建一个新的 Tour loc: [this.lng, this.lat],
这是 MongoDB 中的结果:
如何进行查询以获取半径内的位置?
我尝试在我的控制器中使用这个(现在为了测试目的,radius 和 maxDistance 是硬编码的):
const getTourByRadius = async (req, res) => {
let coords = [];
coords[0] = 9.825031;
coords[1] = 48.625107799999995
const maxDistance = 10;
try {
const tour = await TourDB.Tour.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: coords
},
$maxDistance: maxDistance
}
}
}).exec()
res.send(tour)
} catch (err) {
res.send(err)
}
}
但现在我得到了一个空的 []。
我做错了什么?
【问题讨论】:
-
请勿粘贴图片,源数据使用格式化文本,见meta.stackoverflow.com/a/285557/3027266
标签: javascript angular typescript mongodb mongoose