如何将 .contains 与数组一起使用?

How to use .contains with array?

我在包含用户 ID 列表的文档中有一个数组。
我想找到所有包含此用户 ID 列表的文档。

我知道我可以做类似的事情:

r.db('unicorn')
  .table('rooms').filter(function(doc){
    return doc('people').contains(
      "id-one","id-two"
      )
  })

我知道这会很好用,但我必须硬编码。如何将要匹配的值数组传递给 contains 函数?

您可以使用 setIntersection 并传递数组:

r.db('unicorn')
  .table('rooms').filter(function(doc){
      return doc('people').default([]).setIntersection(
         ["id-one","id-two"]
      ).count().eq(2)
  })

所以,如果你有数组:

var myArr =  ["id-one","id-two"];

你可以这样写查询:

r.db('unicorn')
  .table('rooms').filter(function(doc){
      return doc('people').default([]).setIntersection(myArr).count().eq(myArr.length)
  })

经过一番挖掘,最简单的解决方案是使用 args。从例子中看有点不明显。这样我就可以传入动态值和 运行 命令。

r.db('unicorn')
  .table('rooms').filter(function(doc){
    return doc('people').contains(
      r.args(["id-one","id-two"]) 
      )
  })