Samuel Goldenbaum
Samuel Goldenbaum

Reputation: 18909

RethinkDB: merge and then filter in query

Given a example like

r .table('posts')
  .get(100)
  .merge(function (post) {
    return {
        comments: r.table('comments').getAll(post('id'),
            {index: 'postId'}).coerceTo('array')
    }
 })
 .pluck({"comments": ["id", "created_on"]});

How do I further filter comments to return only comments by a particular user on the given blog post. Ie. Get blog post 100 and return its comments by user_name == 'darth'

Tips greatly appreciated

Upvotes: 1

Views: 381

Answers (1)

Samuel Goldenbaum
Samuel Goldenbaum

Reputation: 18909

Finally went with the following:

r
 .table('posts')
 .get(100)
 .merge(function (post) {
  return {
   comments: r
    .table('comments')
    .getAll(post('id'), {index: 'postId'})
    .coerceTo('array')
    .filter(function (row) {
     return r.expr(['darth', 'luke', 'chewey']).contains(row('user_name'));
    })
  }
 })
 .pluck({"comments": ["id", "user_name", "created_on"]});

Upvotes: 2

Related Questions