2016-11-09 73 views
0

我在包含用户标识列表的文档中有一个数组。
我想查找所有包含此用户标识列表的文档。如何使用.contains与数组?

我知道我可以这样做:

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

我知道这会是不错的,但我有硬编码的东西。如何通过contains函数的一组值来匹配?

回答

1

过了一会儿挖掘,最简单的解决方案是使用args。这些例子有点不明显。这样我就可以传入动态值并与它们一起运行命令。

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

您可以使用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) 
    }) 
+0

这是接近这个问题的一个很巧妙的方法:d。正在尝试一些非常相似的东西,直到我登陆r.args。 – Justin

相关问题