匹配RethinkDB中给定数据数组的至少一个元素

时间:2018-02-19 12:45:34

标签: node.js rethinkdb rethinkdb-javascript

我有一个如下数据集 -

[{
    "name": "new name",
    "tags":[{
      "addedBy":  "userABCe3" ,
      "addedDTS": Tue Jan 23 2018 13:02:37 GMT+00:00 ,
      "tagId":  "08d679c8-31b6-48d3-b8f3-3ab89f7bb821"
      },{
     "addedBy":  "userABCe3" ,
     "addedDTS": Tue Jan 23 2018 13:02:37 GMT+00:00 ,
     "tagId":  "08d679c8-31b6-48d3-b8f3-3ab89f7bb823"
    }]
},
{
    "name": "data 1",
    "tags":[{
     "addedBy":  "userABCe3" ,
     "addedDTS": Tue Jan 23 2018 13:02:37 GMT+00:00 ,
     "tagId":  "08d679c8-31b6-48d3-b8f3-3ab89f7bb823"
     },{
     "addedBy":  "userABCe3" ,
     "addedDTS": Tue Jan 23 2018 13:02:37 GMT+00:00 ,
     "tagId":  "08d679c8-31b6-48d3-b8f3-3ab89f7bb824"
     }]
}]

我需要查询,在给定的tagIds数组中有至少一个匹配的数据

[ "08d679c8-31b6-48d3-b8f3-3ab89f7bb824", "08d679c8-31b6-48d3-b8f3-3ab89f7bb822"]

预期结果如下:

[{
    "name": "data 1",
    "tags":[{
     "addedBy":  "userABCe3" ,
     "addedDTS": Tue Jan 23 2018 13:02:37 GMT+00:00 ,
     "tagId":  "08d679c8-31b6-48d3-b8f3-3ab89f7bb823"
     },{
     "addedBy":  "userABCe3" ,
     "addedDTS": Tue Jan 23 2018 13:02:37 GMT+00:00 ,
     "tagId":  "08d679c8-31b6-48d3-b8f3-3ab89f7bb824"
     }]
}]

提前致谢

1 个答案:

答案 0 :(得分:1)

这个查询对我有用

r.db('dbName').table("tableName").filter(function(doc){
    var tags = doc('tags')('tagId');
    var tagIds = ['08d679c8-31b6-48d3-b8f3-3ab89f7bb881','08d679c8-31b6-48d3-b8f3-3ab89f7bb823' ];
    var query = tagIds.map(function(tagId){
      return tags.contains(tagId)
    });
    return r.expr(query).contains(true)
  })