rethinkdb - hasFields to find all documents with m

2019-08-02 14:46发布

问题:

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

回答1:

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));
})


回答2:

I just realized I can chain multiple .filter commands. Here's what worked for me:

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

My next quest: put all of these into an array and then feed the email addresses in batch