在MongoDB map-reduce中计算距离

ypr*_*rez 3 mapreduce distance mongodb

我有一个具有地理索引的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
    ]
}
Run Code Online (Sandbox Code Playgroud)

我在其上运行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}})
Run Code Online (Sandbox Code Playgroud)

location在地图功能中,有没有一种方法可以计算到X点之间的距离?

我正在寻找类似$ geoNear的东西,但以某种方式将其与map-reduce结合使用。

例如:

db.runCommand({geoNear: "coll", near: [-74, 40.74], spherical: true})
Run Code Online (Sandbox Code Playgroud)

返回每个文档的距离。但是我找不到将其与map-reduce命令结合使用的方法。

ast*_*anu 5

大圈子惯例是一种去世的方式http://en.wikipedia.org/wiki/Great-circle_distance

我遇到了与mongo和js类似的问题。并想出了这个功能。希望能帮助到你。

function find(point, latlng, radius){
       var dist = parseInt(radius) * 0.868976 / 60; // convert miles to rad

    if((point[0] <= latlng[0] + dist && point[1] >= latlng[1]- dist) 
    && (point[0] <= latlng[0]+ dist && point[1] >= latlng[1]- dist)){

        dx = latlng[0] - point[0];
        dy = latlng[1] - point[1];
        dx *= dx;
        dy *= dy;
        ds = dx + dy;
        rs = dist * dist;
        is =  ds <= rs;

        return is;
    }
}
Run Code Online (Sandbox Code Playgroud)

我这样称呼:

find([-79,5], [40,20], 5);
Run Code Online (Sandbox Code Playgroud)