Search code examples
rethinkdbrethinkdb-javascript

RethinkDB: Filter documents based on filtered field


I have a table of documents (entity) with a field that is a list of objects:

{
    "field": { 
        "content": [
            { "url": "https://www.google.com" },
            { "url": "" }
        ]
},
{
    "field": {
        "content": [
            { "url": "" }
        ]
}

I'd like to filter the table to get only those documents that have valid content. Valid content is defined as a content field that contains at least one item with a non-blank url.

I've tried this, but it complains about the inner filtering function:

r.db('my_db').table('entity').filter(function (row){
  return row({'field': 'content'}).filter(function (item) {
    return item.hasField('url')
      .and(item('url').ne(''))
  }).count().gt(0)
})

Solution

  • The problem was not the filtering - I was accessing the nested field incorrectly.

    This works:

    r.db('my_db').table('entity').filter(function (row){
      return row('field')('content').filter(function (item) {
        return item.hasField('url')
          .and(item('url').ne(''))
      }).count().gt(0)
    })