【发布时间】:2014-02-26 14:09:13
【问题描述】:
我有一个带有地理索引的 MongoDB 集合:
> db.coll.getIndexes()
[
// ...
{
"v" : 1,
"key" : {
"location" : "2dsphere"
},
"ns" : "test.coll",
"dropDups" : false,
"name" : "location_2dsphere",
"background" : false
}
]
db.coll.findOne({location: {'$exists': true}}, {'location': 1})
{
"_id" : ObjectId("52cd72ae2ac170aa3eaace6e"),
"location" : [
55.4545177559,
11.5767419669
]
}
我正在运行 map reduce,它看起来像这样:
var map = function() {
var value = 0;
// ... various calculations on the value here
var distance = 0; // < This is the problematic part
if (distance < 1000) {
val += distance; // for example
}
emit(this._id, value)
}
var reduce = function(id, val) {
return {id: val}
}
db.coll.mapReduce(map, reduce, {out: {inline: 1}})
有没有办法在map函数中计算location和X点之间的距离?
我正在寻找类似 @987654321@ 的东西,但不知何故与 map-reduce 相结合。
例如:
db.runCommand({geoNear: "coll", near: [-74, 40.74], spherical: true})
返回每个文档的距离。但我找不到将它与 map-reduce 命令结合使用的方法。
【问题讨论】:
-
另外,你已经尝试了什么?
-
用纯 js 做并不是最优的,因为 mongo 已经支持距离计算...
-
至于“我尝试过的”,我在集合上运行
geonear查询和map-reduce。寻找一种方法来结合 2. -
@YuriPrezument 你也可以在
mapReduce中限定what你正在尝试做的事情,而不是仅仅问“我可以使用它吗”,也许有人可能会建议“你正在尝试做什么”。到目前为止,我们还看不到这一点。请注意,您在近距离投票名单上,因此最好确定您想要什么。
标签: mongodb mapreduce distance