Julio Marins
Julio Marins

Reputation: 10639

How to filter and delete a record that has a specific attribute

Im trying filter records with that has a specific value key and delete them. I tried "withFields" and "hasFields" but seems that i can't apply delete to them. My question is how can i do that?

Upvotes: 1

Views: 178

Answers (2)

Jorge Silva
Jorge Silva

Reputation: 4614

If you want all documents that have a type key, you can use hasFields for that.

r.db('databaseFoo').table('checkpoints')
 .hasFields('type')

In your current query, what you are doing is getting all documents that don't have a type key or where the value for type is equal to false. This might be what you want, but it's a little confusing if you only want documents that have a type property.

Keeping a reference to the original document

The problem with using hasFields is that it converts a selection (a sequence with a reference to the specific rows in the database) that you can update, and delete into a sequence, with which you can't do that. This is a known issue in RethinkDB. You can read this blog post to understand the different types in ReQL a bit better.

In order to get around this, you can use the hasFields method with the filter method.

r.db('databaseFoo').table('checkpoints')
 .filter(r.row.hasFields('type'))
 .delete()

This query will work since it returns a selection which can then be passed into delete.


If you want to get all records with with a specific value at a specific key, you can do so a couple of different ways. To get all documents where the property type is equal to false, you can do as follows:

r.db('databaseFoo').table('checkpoints')
 .filter({ type: false })

or, you can do:

r.db('databaseFoo').table('checkpoints')
 .filter(r.row('type').eq(false))

Upvotes: 1

Julio Marins
Julio Marins

Reputation: 10639

r.db('databaseFoo').table('checkpoints').filter(function (user) {
    return user('type').default(false);
}).delete();

Upvotes: 1

Related Questions