【发布时间】:2014-05-02 16:06:38
【问题描述】:
给定一个为包含 GeoJSON 位置的文档定义的架构;
var BranchSchema = new Schema({
location: {
'type': {
type: String,
required: true,
enum: ['Point', 'LineString', 'Polygon'],
default: 'Point'
},
coordinates: [Number]
},
name: String
});
BranchSchema.index({location: '2dsphere'});
还有一些示例数据:
[
{
"name": "A",
"location": {
"type": "Point",
"coordinates": [153.027117, -27.468515 ] //Brisbane, Australia
}
},
{
"name": "B",
"location": {
"type": "Point",
"coordinates": [153.029884, -27.45643] //Also Brisbane, Australia
}
}
]
以下 geoNear 查询未按预期运行。我将此查询读为 “给定一个位于南美洲海岸的位置,在这些位置中进行拖网搜索,找到距离所提供位置 1 米范围内的任何位置。”
// Somewhere off the east coast of South America.
var point = {type: 'Point', coordinates: [0.0776590, -33.7797590]};
Branch.geoNear(point, {maxDistance:1, spherical: true}, function (err, data) {
...
// at this point I expected data.length === 0.
// Instead it is returning both documents.
...
});
我做错了什么?
- 根据 WGS84 标准定义位置时,我使用 [long,lat]。
- 运行 MongooseJS V3.8.8
【问题讨论】:
-
"此时我期望 data.length === 0" ?您为什么要在这里提供一份文件?您不会在任何地方限制结果。
-
Jayram 我不希望有任何结果,因为集合中的两个文档都应该在 [0.0776590, -33.7797590] 的 maxDistance 1 之外。我认为我的查询表达式有问题,但我不知道是什么:-)
-
我在 mongoose 的 geonear 上遇到了同样的问题,因为它不起作用。我建议你手动或使用 $near。
标签: node.js mongodb mongoose geocoding