Search code examples
rethinkdbrethinkdb-javascript

rethinkdb - hasFields to find all documents with multiple multiple missing conditions


I found an answer for finding all documents in a table with missing fields in this SO thread RethinkDB - Find documents with missing field, however I want to filter according to a missing field AND a certain value in a different field.

I want to return all documents that are missing field email and whose isCurrent: value is 1. So, I want to return all current clients who are missing the email field, so that I can add the field.
The documentation on rethink's site does not cover this case.

Here's my best attempt:

r.db('client').table('basic_info').filter(function (row) {
  return row.hasFields({email: true }).not(),
/*no idea how to add another criteria here (such as .filter({isCurrent:1})*/

  }).filter

Solution

  • Actually, you can do it in one filter. And, also, it will be faster than your current solution:

    r.db('client').table('basic_info').filter(function (row) {
        return row.hasFields({email: true }).not()
          .and(row.hasFields({isCurrent: true }))
          .and(row("isCurrent").eq(1));
    })
    

    or:

    r.db('client').table('basic_info').filter(function (row) {
        return row.hasFields({email: true }).not()
          .and(row("isCurrent").default(0).eq(1));
    })