Skip to content

Instantly share code, notes, and snippets.

@steveny
Created January 15, 2014 18:41
Show Gist options
  • Save steveny/8441838 to your computer and use it in GitHub Desktop.
Save steveny/8441838 to your computer and use it in GitHub Desktop.
use geospatial
elng=-45.929887
slat=-2.940540
nlat=73.844279
wlng=-143.255854
wsize = (wlng < elng) ? elng-wlng : (180-wlng)+(180-Math.absw(elng))
hsize = nlat-slat
csize = (wsize > hsize) ? wsize/3 : hsize/3
db.dispensers.find({"loc": {$within: {$box: [[wlng,slat], [elng,nlat]]}},
"product_id": 41,
"business_type": {$in: [1,2] }})
map = function() {
vindex = Math.floor((this.loc.x+90)/csize)
hindex = Math.floor((this.loc.y+180)/csize)
key = hindex + ":" + vindex
emit(key, this.id)
}
reduce = function(key, ids) {
result = {a:[]}
ids.forEach(function(id) {
if(result.a.size < 10) {
result.a.push(id)
}
})
return result;
}
db.dispensers.mapReduce(map,
reduce,
{
"out": {"inline": 1},
"scope": {"csize": csize},
"query": {"loc": {$within: {$box: [[wlng,slat], [elng,nlat]]}},
"product_id": 41,
"business_type": {$in: [1,2] }}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment