Search code examples
sqlcountdistinctrethinkdbreql

MySQL distinct count type query with RethinkDB


I'm having some problems implementing the following SQL query in rethinkdb, I wanna get the 5 most popular channels in a community, based on user_count.

SELECT 
    channels.*, 
    COUNT(distinct channel_users.user_id) as user_count 
FROM channel_users 
LEFT JOIN channels ON 
    channels.id = channel_users.channel_id 
WHERE channels.community_id = "MY_COMMUNITY_ID" AND channels.type = 'public' 
GROUP BY channel_id 
ORDER BY user_count DESC 
LIMIT 5

This is what I got this far in ReQL, which just gives me a list of the channels, I suspect some more map/reducing is required here?

r.db('my_db')
.table('channel_users')
.filter({ community_id : 'MY_community_id' })
.orderBy(r.desc('created_at'))
.eqJoin('channel_id', r.table('channels'))
.map(function(doc){ 
    return doc.merge(function(){ 
        return {
            'left' : null,
            'right': {'user_id': doc('left')('user_id')}
        }
    })
})
.zip()
.run(function(err, channels){
    console.log(err, channels);
    next();
});

And the table design looks like:

channel_users

id | channel_id | community_id | role | user_id

channels

id | community_id | name | user_id (creator)

Any help appreciated! Thanks


Solution

  • Does this do what you want?

    r.table('channels').filter(
      {community_id: 'MY_COMMUNITY_ID', type: 'public'}
    ).merge(function(channel) {
      return {user_count: r.table('channel_users').filter({channel_id: channel('id')}).count()};
    }).orderBy(r.desc('user_count')).limit(5)
    

    (Note that you can speed this up by using getAll instead of filter inside the merge if you create a secondary index on channel_id.)