Created
November 23, 2012 03:04
-
-
Save chouqin/4133840 to your computer and use it in GitHub Desktop.
mongodb mapreduce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bson.code import Code | |
from bson.son import SON | |
def get_user_num_group_by_region_id(): | |
map = Code("\ | |
function () {\ | |
emit({region_id : this.region_id, phone: this.phone}, 1);\ | |
}\ | |
") | |
reduce = Code("\ | |
function (key, values) {\ | |
return 1;\ | |
}\ | |
") | |
map2 = Code("\ | |
function () {\ | |
emit(this['_id']['region_id'], 1);\ | |
}\ | |
") | |
reduce2 = Code("\ | |
function (key, values) {\ | |
var count = 0;\ | |
for (var i = 0; i < values.length; ++i) {\ | |
count += values[i];\ | |
}\ | |
return count;\ | |
}\ | |
") | |
#result = region_active_user_daily.map_reduce(map, reduce, out=SON([("replace", "region_phone"), ("db", "active_user_stats")]), full_response=True) | |
#result = region_active_user_daily.map_reduce(map, reduce, "myresult", query={"order_date" : {"$gt" : "2012-10-20", "$lt" : "2012-11-20"}}) | |
result = region_active_user_daily.map_reduce(map, reduce, "myresult", full_response=True) | |
print result | |
exit(0) | |
#region_phone = connection.active_user_stats.region_phone | |
#result1 = region_phone.map_reduce(map2, reduce2, "results") | |
result1 = result.map_reduce(map2, reduce2, "results") | |
for doc in result1.find(): | |
print doc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment