【发布时间】:2014-05-28 03:34:19
【问题描述】:
我正在尝试查询给定 lat/lng 范围内的数据点。你能像我所做的('location.lat')和('location.long')那样在猫鼬查询中引用对象的元素吗?如果是这样,我没有从这个查询中得到任何数据。我正在查询所有数据点,而且效果很好 - 现在我只是尝试将查询细化为仅给定范围内的点。
编辑
查询具有新格式的点(请参阅下面的更新架构):
var range = {"topLeft":[-113.51526849999999,53.24204911518776],"bottomRight":[-131.0933935,41.397215826886736],"topRight":[-131.0933935,53.24204911518776],"bottomLeft":[-113.51526849999999,41.397215826886736]};
db.datapoints.find({
geo: {
$geoWithin : {
$geometry: {
type: "Polygon",
coordinates: [
[
range.topLeft, range.topRight, range.bottomRight, range.bottomLeft
]
]
}
}
}
})
但我得到了:
error: {
"$err" : "Malformed geo query: { $geoWithin: { $geometry: { type: \"Polygon\", coordinates: [ [ [ -113.5152685, 53.24204911518776 ], [ -131.0933935, 53.24204911518776 ], [ -131.0933935, 41.39721582688674 ], [ -113.5152685, 41.39721582688674 ] ] ] } } }",
"code" : 16677
}
注意:范围参数如下所示:
更新的架构
var mongoose = require('mongoose');
var dataPointSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
geo: {type: [Number], index: '2d'},
...
timestamp: {type: Date, default: Date.now}
...
});
【问题讨论】:
-
这是什么版本?你有位置索引吗?您可以使用 MongoDB 的地理空间功能,但它希望数据以相反的顺序存储:长、纬度。
-
v2.4.8 - 我开始研究 mongo 地理空间点 - 所以 Schema 只需将位置切换为 {long: Number, lat: Numer} 或 {type: [Number], index: '2d '}
-
已解决。多边形的第一个点和最后一个点必须相同(您必须关闭多边形。(我在这里找到了这个:stackoverflow.com/questions/18841687/…)