RethinkDB:​​RqlRuntimeError:无法对序列序列执行括号

时间:2014-12-10 16:26:46

标签: rethinkdb

在表格中给出以下文件:

[
  {
    "id": "d30aa369-99d6-4a40-9547-2cbdf0bb069a",
    "locations": [
      {
        "alerts": [
          {"person": 200},
          {"person": 300}
        ],
        "name": "home"
      },
      {
        "alerts": [
          {"person": 200},
          {"person": 300}
        ],
        "name": "work"
      }
    ],
    "person": 100
  },
  {
    "id": "d671d2c7-0e0d-43c0-9148-9e6b049b49a7",
    "locations": [
      {
        "alerts": [
          {"person": 100},
          {"person": 300}
        ],
        "name": "home"
      },
      {
        "alerts": [
          {"person": 100},
          {"person": 300}
        ],
        "name": "work"
      }
    ],
    "person": 200
  }
] 

我想返回所有包含特定人物警报的位置。我认为这会有点像这样:

r.db('db').table('test')('locations').filter(function(location){
  return location('alerts')('person').eq(200);
});

但我得到了:

  

RqlRuntimeError:无法对以下序列序列执行括号:

执行此查询的正确方法是什么?

1 个答案:

答案 0 :(得分:5)

每当你得到"序列序列"错误,解决方案通常涉及使用concatMap

在这种情况下,我认为这会让你得到你想要的东西:

r.db('db').table('table')
    .concatMap(r.row('locations'))
    .filter(r.row('alerts')('person').contains(200))
具有身份函数concatMap

function(x){return x}也称为" flatten" (虽然没有一个名为flatten的reql术语)

编辑:

如果要返回整个顶级文档,则需要将concatMap向下推进一步,然后使用" flatten"我上面提到的成语:

r.db('db').table('table').filter(function(person){
    return person
      ('locations')
      ('alerts')
      .concatMap(function(x){return x}) // flatten
      ('person')
      .contains(200)
})