【发布时间】:2014-09-16 18:03:18
【问题描述】:
以下查询应返回 lng 和 lat 距离内的城市。城市集合在其 gps 字段上有一个 2dsphere 索引。
City.native(function(err, collection) {
collection.aggregate([{
$geoNear: {
near: {
type: 'Point',
coordinates: [lng, lat]
},
distanceField: 'dist',
limit: limit,
maxDistance: distance,
distanceMultiplier: 1,
spherical: true
}
}], function(err, result) {
return res.send(result);
}
});
});
我的问题是,当我将距离设置为 0 时,查询仍然返回带有例如的文档
"dist": 27507.15237596358
但它不应该返回任何东西,因为在 0 的距离内没有城市。知道我做错了什么吗?
查询:
lat=51.9637151&lng=8.5610982&distance=1
返回具有以下位置的文档:
"gps": {
"type": "Point",
"coordinates": [
8.906756,
52.030319
]
},
"dist": 24824.18378549408
但 maxDistance 设置为 1。
在我在上面的代码中使用聚合之前,我使用了这段代码并且它有效:
City.native(function(err, collection) {
collection.geoNear(lng, lat, {
limit: limit,
maxDistance: maxDistance / 6371, // in km
distanceMultiplier: 6371, // converts radians to miles (use 6371 for km)
spherical: true
}, function(err, result) {});
但是,当我更改为聚合时,它停止工作。
【问题讨论】:
-
[para]"设置为 0 应该不会返回任何结果..." 为什么要这样做?将其设置为 0 以外的任何值,它将按预期工作。
-
@NeilLunn 即使我将其设置为 1,它也会返回所有文档而不是零文档
-
无法复制。 [ 151.00211262702942, -33.81696995135973 ] 距离 [ 150.92094898223877, -33.77654333272719 ] 为 8753 米。任何低于该值的数字都不会选择该文档。但是
0当然会这样做,因为您从不查询故意没有结果。也许您应该显示一些数据,这可能是您的问题。 -
@NeilLunn 看看我更新的问题。
-
仍然无法复制。你实际上有“maxDistance”而不是“maxdistance”吗?而且这个集合中只有一个“2dsphere”索引
标签: node.js mongodb node-mongodb-native