python - Migrating to MongoDB: how to query GROUP BY + WHERE -
i have mysql table records of people's names , time of arrival expresed number. think of marathon. want know how many people arrived gap of time named same, so:
select name, count(*) mydb.mytable time>=100 , time<=1000 group name
and results get:
susan, 1 john, 4 frederick, 1 paul, 2
i'm migrating mongodb now, , using python code (so i'm asking pymongo help). tried looking information group equivalent (even when have read nosql databases worse @ kind of operation sql ones), since released new agreggate api, hjaven't been able find simple example solved group method, map-reduce method or brand new agreggate api.
any help?
there examples of on documentation, google , site.
some references:
- http://api.mongodb.org/python/current/examples/aggregation.html
- http://docs.mongodb.org/manual/reference/aggregation/group/
- http://docs.mongodb.org/manual/reference/aggregation/sum/
and code:
self.db.aggregate( # lets find our records {"$match":{"time":{"$gte":100,"$lte":1000}}}, # lets group on name counting how many grouped documents have {"$group":{"_id":"$name", "sum":{"$sum":1}}} )
Comments
Post a Comment