Rethinkdb谓词函数“包含”多个值不起作用

Rethinkdb predicate function 'contains' for multiple values not working

本文关键字:不起作用 包含 谓词 函数 Rethinkdb      更新时间:2023-09-26

我有一个看起来像这样的数据架构:

{
  "datetime": "1453845345493",
  "someIds": ["id2000-4", "id1000-34"]
}

其中数组someIds可以包含许多不同的值。

使用以下查询基于一个 id 进行筛选没有问题,并返回大约 400 个结果。(我也计划实现适当的索引,但现在正在测试一个更简单的情况。

r.db("dbName").table("tableName")
  .filter(r.row("datetime").gt(1453845340493))
  .filter(function(someVal){
    return someVal("someIds").contains("id2000-4");
  })
  .count()

但是尝试遵循"包含"文档中的最后一个示例,它建议使用谓词函数来模拟"or"语句,当它应该返回上述超集时,什么也未返回:

r.db("dbName").table("tableName")
  .filter(r.row("datetime").gt(1453845340493))
  .filter(function(someVal){
    return r.expr(["id2000-4", "id1000-77"]).contains(someVal("someIds"));
  })
  .count()

据我所知,我已经完全按照该页面上的说明进行操作,但它不起作用。我正在运行 RethinkDB v2.2.3-1,并在数据资源管理器中运行它。有什么建议吗?

该页面上的示例调用r.expr(ARRAY).contains(SINGLE_VAL),而您的代码调用r.expr(ARRAY).contains(OTHER_ARRAY) 。 如果您想检查someIds是否包含数组中的任何元素,我会为此使用 https://www.rethinkdb.com/api/javascript/set_intersection/。 (所以像.filter(function(someVal) { return r.expr(ARRAY).setIntersection(someVal('someIds')).count().ne(0); })这样的东西。

如果你想用contains来做,你可以这样写:

.filter(function(someVal) {
  return someVal('someIds').contains(function(someId) {
    return r.expr(ARRAY).contains(someId);
  });
})