Skip to content

Instantly share code, notes, and snippets.

@Ray901
Last active August 29, 2015 14:08
Show Gist options
  • Save Ray901/71e6aad3d7329b529498 to your computer and use it in GitHub Desktop.
Save Ray901/71e6aad3d7329b529498 to your computer and use it in GitHub Desktop.
from pymongo import Connection
import datetime
setPath="ec2-54-248-156-148.ap-northeast-1.compute.amazonaws.com"
setDBS="ehanlin_103_jhs_db"
conn=Connection(setPath,27017)
db = conn[setDBS]
pipe = [
{"$match" : {"date":{"$gt":datetime.datetime(2014,10,21,23,59,59),"$lte":datetime.datetime(2014,10,22,23,59,59)}}},
{
"$group" : {
"_id":{
"user":"$user",
"year":{"$year":"$date"},
"month":{"$month":"$date"},
"day":{"$dayOfMonth":"$date"},
"subject":"$subject",
"volume":"$volume"
},
"examCount":{"$sum":{"$cond":[{"$gte":["$exam",1]},"$exam",0]}},
"doCount":{"$sum":{"$cond":[{"$gte":["$exam",1]},"$total",0]}},
"rightCount":{"$sum":{"$cond":[{"$gte":["$exam",1]},"$right",0]}},
"slipRight":{"$sum":{"$cond":[{"$eq":["$type","slip"]},"$right",0]}},
"videoCount":{"$sum":{"$cond":[{"$eq":["$type","video"]},"$total",0]}},
"progressVideoCount":{"$sum":{"$cond":[{"$eq":["$type","progressVideo"]},"$total",0]}},
"animationCount":{"$sum":{"$cond":[{"$eq":["$type","animation"]},"$total",0]}},
"labVideoCount":{"$sum":{"$cond":[{"$eq":["$type","labVideo"]},"$total",0]}}
}
},
{"$sort" : {"doCount" : 1}}
]
result=db.HourlyUnitCounters.aggregate(pipeline=pipe)
for doc in result['result']:
print(doc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment