#RethinkDB – Is my (nested) field value contained in an array?

Hey guys,

For those of you RethinkDB adopters out there, finding out whether a field -being an array- contains a value could be as easy as:

//...
.filter(function(row) {
  return row('fieldName').contains('someValue');
});

But what if we want kind of “the opposite”, e.g., whether our field value is a member of an array? Turns out it’s not as obvious, but still very doable – even for nested fields. Take a look at this:

var list = ['something', 'something else'];
//...
.filter(function(row) {
  return r.expr(list).contains(row('myNested')('fieldName'));
})

And that’s it.

Since the case wasn’t clearly documented, I hope this could be of some help to somebody out there, or better yet, someone would come up with a canonical / more optimized approach? Look forward to hearing from you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s