给定表格中的以下文档:
[
{
"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:无法对以下序列中的序列执行括号:
执行此查询的正确方法是什么?
每当出现“序列的序列”错误时,解决方案通常涉及使用连续映射 http://rethinkdb.com/api/javascript/concat_map/.
在这种情况下,我认为这会给你带来你想要的:
r.db('db').table('table')
.concatMap(r.row('locations'))
.filter(r.row('alerts')('person').contains(200))
concatMap
与恒等函数function(x){return x}
也称为“展平”(尽管没有真正的术语“展平”)
Edit:
如果你想返回整个顶级文档,你需要将 concatMap 进一步向下推,并使用我上面提到的“展平”习惯用法:
r.db('db').table('table').filter(function(person){
return person
('locations')
('alerts')
.concatMap(function(x){return x}) // flatten
('person')
.contains(200)
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)