map/reduce to get a bounding box of geo-indexed docs

31 views
Skip to first unread message

Daniel W

unread,
Oct 31, 2011, 12:15:39 PM10/31/11
to mongodb-user
I have a collection with this index.

"key" : {
"g" : "2d",
"c" : 1,
"s.s" : 1
}


I'm trying to determine the bounding box for all docs with a certain
value of c, so that I can randomly sample from within that box.
I wrote a map/reduce in python, and when I run it, it just hangs for
hours, even on a query that returns only 2 documents. Is there a bug
in my map/reduce? Is there a better way to do this?

def boundingbox(campaign, collection):
map = Code("function () {"
" emit ('minlt', this.g[1]);"
" emit ('maxlt', this.g[1]);"
" emit ('minln', this.g[0]);"
" emit ('maxln', this.g[0]);"
"}")

reduce = Code("function (key, values) {"
" if (key=='maxlt' || key=='maxln') { "
" var thismax=-180;"
" for (var i = 0; i < values.length; i++) {"
" thismax = Math.max(thismax,values[i]);"
" }"
" return thismax;"
" }"
" else {"
" var thismin=180; "
" for (var i = 0; i < values.length; i++) {"
" thismin = Math.min(thismin,values[i]);"
" }"
" return thismin;"
" }"
"}"

)
resultcoll = collection.map_reduce(map, reduce, "myresults",
query={'c':campaign})
results=resultcoll.find()
results = [row for row in results]
return results

Spencer T Brody

unread,
Oct 31, 2011, 6:47:22 PM10/31/11
to mongod...@googlegroups.com
At first glance that seems correct to me.  Take a look at db.currentOp() to see what's happening while the MR is running, and also look at the mongod logs and see if you see any warnings or error messages.
Reply all
Reply to author
Forward
0 new messages