OR query in rethink

时间:2015-06-26 09:43:21

标签: arrays rethinkdb

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 个答案:

答案 0 :(得分: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'));
})

答案 1 :(得分:1)

正如mlucy所说,您可以将filterorcontains结合使用,以获取具有特定标记的所有文档。

如果您有一系列标签,您也可以执行以下操作:

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

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

这比使用or更容易扩展。

多指数

您还可以为该属性创建multi-index。多索引允许您按数组中的任何值查找数据。所以,例如:

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

此查询将获取具有“年轻”和“板球”标记的所有文档,并且它比使用嵌套的contains更高效,更清晰。