Skip to content

Instantly share code, notes, and snippets.

@hubertlepicki
Created December 29, 2009 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hubertlepicki/265405 to your computer and use it in GitHub Desktop.
Save hubertlepicki/265405 to your computer and use it in GitHub Desktop.
lat, lng = [53, 21]
conditions = { :page => 1, :per_page => 20 }
map = "function() { emit(this._id, {lat: this.lat, lng: this.lng}); }"
reduce = <<BEGIN
function(key, values) {
distance = 0;
values.forEach(function(doc) {
distance = (doc.lat - #{lat})*(doc.lat - #{lat}) + (doc.lng - #{lng})*(doc.lng - #{lng});
});
return distance;
};
BEGIN
results = Place.collection.map_reduce(map, reduce).find({}, {:sort => "value ASC", :limit => conditions[:per_page]})
conditions[:_id] = { "$in" => results.collect { |r| r["_id"]} }
end
Place.paginate(conditions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment