鉴于表中的以下文件:
[
{
"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
}
]
Run Code Online (Sandbox Code Playgroud)
我想返回所有包含特定人员警报的位置.我觉得它会像这样:
r.db('db').table('test')('locations').filter(function(location){
return location('alerts')('person').eq(200);
});
Run Code Online (Sandbox Code Playgroud)
但我得到:
RqlRuntimeError:无法对以下序列序列执行括号:
执行此查询的正确方法是什么?
每当你得到"序列序列"错误时,解决方案通常涉及使用concatMap.
在这种情况下,我认为这会得到你想要的:
r.db('db').table('table')
.concatMap(r.row('locations'))
.filter(r.row('alerts')('person').contains(200))
Run Code Online (Sandbox Code Playgroud)
concatMap使用identity函数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)
})
Run Code Online (Sandbox Code Playgroud)