Calculate distance in map-reduce MongoDB

I have a MongoDB collection with a geo index:

> 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
    ]
}

On which I run a map reduction that looks something like this:

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}})

Is there a way to calculate the distance between locationand the point X in the display function?

I am looking for something like $ geoNear , but somehow combined with map reduction.

For instance:

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

Returns the distance for each document. But I can’t find a way to combine it with the map-reduce command.

+3
source share
2 answers

The Great Circle Formula is the Way http://en.wikipedia.org/wiki/Great-circle_distance

I ran into a similar problem with mongo and js. And came up with this feature. Hope this helps.

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;
    }
}

:

find([-79,5], [40,20], 5);
+4
+1

All Articles