0

Is it possible to do OR search on values of array in Rethink.

Eg: I have table called "post" with field "tags" which is an array. I want to do an OR search on values of tags.

post1 {
tags : ["young","agreesive"]
}

post2 {
tags : ["cricket","India"]
}

Give me all posts which contains tags "young" or "cricket" should return be both the posts.

2 Answers2

2

As mlucy said, you can use filter in conjunction with or and contains to get all documents with a specific tag.

If you have an array of tags, you can do the following too:

var tags = ["young", "cricket"];

r.table('posts').filter(function (row) {
  return r.expr(tags).contains(function (value) { 
    return row("tags").contains(value) 
  });
})

This is more easily extensible than using or.

Multi Index

You can also create a multi-index for that property. Multi-indexes let you lookup data by any of the values in an array. So, for example:

r.table("posts").indexCreate("tags", {multi: true})
r.table("posts").getAll("young", "cricket", {index: "tags"})

This query would get all the documents with the "young" and "cricket" tag, and it's more performant and cleaner than using the nested contains.

Jorge Silva
  • 4,574
  • 1
  • 23
  • 42
1

You can do that with or and contains:

r.table('posts').filter(function(row) {
  return row('tags').contains('young').or(row('tags').contains('cricket'));
})
mlucy
  • 5,249
  • 1
  • 17
  • 21