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;
var distance = 0;
if (distance < 1000) {
val += distance;
}
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.
yprez source
share