use:
- python3
- pymongo3 library
import pymongofrom datetime import datetime, timedeltafrom pymongo.code import CodeMAP_FUNCTION = Code("function () { emit(this.player, { count:1, score:this.score} ); }")REDUCE_FUNCTION = Code("function (key, values) {var result = {count:0, score:0}; values.forEach(function (value) {result.count += value.count; result.score += value.score;}); return result; }")dbhost="" dbport=27017mongodb = pymongo.Connection(dbhost, dbport)db = mongodb.age=10query={'last_visited': {'$gte': datetime.utcnow()-timedelta(age)}}result = db..map_reduce(map=MAP_FUNCTION, reduce=REDUCE_FUNCTION, out="output", query=query)
for item in result.find():player = item["_id"]score = item["value"]['score']print(player, score)
No comments:
Post a Comment