javascript - get the count and group them -
i'm using python pymongo.
in 1 of mongo collection saving different messages different countries. every document have 1 country short code indicate country. may know how can group them , count every country code?
for example
{u'tweet': u"if you're male , own iphone, new star soccer. holy shit it's addictive", u'loc': u"us", u'_id': objectid('515ecace4e18187ca67ddfcb'), u'time': datetime.datetime(2013, 4, 5, 12, 59, 57)}
above 1 row of message location, , in database may have us, sg, au , on... different locations have differences number of messages. how can query or possible approach? thanks
update
the reason want count because need inject data following code google chart generate chart me.
var data = google.visualization.arraytodatatable([ ['country', 'popularity'], ['germany', 200], ['united states', 300], ['brazil', 400], ['canada', 500], ['france', 600], ['ru', 700] ]);
use aggregation framework.
db.collection.aggregate({$group:{_id:"$loc", count: {$sum:1}}}
this give each country how many time mentioned.
Comments
Post a Comment