Skip to content

Instantly share code, notes, and snippets.

@peterbe
Created August 17, 2011 16:11
Show Gist options
  • Save peterbe/1151892 to your computer and use it in GitHub Desktop.
Save peterbe/1151892 to your computer and use it in GitHub Desktop.
Test
from bson.code import Code
map_ = Code("""
function() {
if (this.right)
emit('right', {total:this.time, count:1});
if (!this.right && this.answer)
emit('wrong', {total:this.time, count:1});
}
""")
reduce_ = Code("""
function r( who , values ){
var n = { total : 0 , count : 0 };
for ( var i=0; i<values.length; i++ ){
n.total += values[i].total;
n.count += values[i].count;
}
return n;
}
""")
finalize = Code("""
function f( who , res ){
res.avg = res.total / res.count;
return res;
}
""")
...
result = db.PlayedQuestion.collection.map_reduce(
map_, reduce_, 'myoutput',
finalize=finalize,
query={'play.$id': {'$in': play_ids},
'time': {'$ne': None},
}
)
times = {}
for reduction in result.find():
if reduction['_id'] == 'right':
times['right'] = reduction['value']['avg']
elif reduction['_id'] == 'wrong':
times['wrong'] = reduction['value']['avg']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment