Search code examples
rethinkdbrethinkdb-python

Limit and rank a group count in RethinkDB


My documents contain 7 fields, one of them is "city", values are different cities. When I do

r.db('base').table('table').group('city').count().run()

RethinkDB groups the number of documents by cities. The output is a list of 277 cities each one being associated with a number of documents. I'd like to have the same output, except that I'd only want to see cities associated with 96 or more documents.

For example, when the current query output is

{(u'Newtown Square',): 2, (u'Delhi',): 242, (u'Emeryville',): 19, (u'Chicago',): 96},

I'd like the new one to give

{(u'Delhi',): 242, (u'Chicago',): 96}

If possible, I'd also like the result to be ranked by descending order.


Solution

  • Something like this would do it: r.db('base').table('table').group('city').count().ungroup().filter(function(row) { return row('reduction').ge(96); }).orderBy(r.desc('reduction')).