Dus*_*ell 9 javascript rethinkdb
尝试使用此示例连接ID数组:https://github.com/rethinkdb/rethinkdb/issues/1533#issuecomment-26112118
存储表格片段
{
"storeID": "80362c86-94cc-4be3-b2b0-2607901804dd",
"locations": [
"5fa96762-f0a9-41f2-a6c1-1335185f193d",
"80362c86-94cc-4be3-b2b0-2607901804dd"
]
}
Run Code Online (Sandbox Code Playgroud)
位置表格代码段
{
"lat": 125.231345,
"lng": 44.23123,
"id": "80362c86-94cc-4be3-b2b0-2607901804dd"
}
Run Code Online (Sandbox Code Playgroud)
我想选择商店并加入他们的商店位置.
来自ReThinkDB贡献者的原始示例:
r.table("blog_posts")
.concat_map(lambda x: x["comment_ids"].map(lambda y: x.merge("comment_id" : y)))
.eq_join("comment_id", r.table("comments"))
Run Code Online (Sandbox Code Playgroud)
我试图转换为JS
r.table("stores")
.concatMap((function(x){
return x("locations").map((function(y){
return x("locations").add(y);
}))
}))
.eqJoin("locations", r.table("locations"))
Run Code Online (Sandbox Code Playgroud)
结果
RqlRuntimeError: Expected type ARRAY but found STRING
Joe*_*ner 11
您正在使用concatMap错误,这是您想要查询的第一部分.
r.table("stores")
.concatMap(function (x) {
return x("locations");
})
Run Code Online (Sandbox Code Playgroud)
尝试运行它,它应该给你:
["5fa96762-...", "80362c86-...", ...]
Run Code Online (Sandbox Code Playgroud)
现在我们需要将它连接到另一个表.要将一个id数组连接到一个表,你可以像这样使用eqjoin:
array.eqJoin(function (row) { return row; }, table)
Run Code Online (Sandbox Code Playgroud)
这里有更多细节:rql从javascript中的密钥rethinkdb列表中获取多个文档.
总而言之,我们得到:
r.table("stores")
.concatMap(function (x) {
return x("locations")
})
.eqJoin(function (i) { return i; }, r.table("locations"))
Run Code Online (Sandbox Code Playgroud)
要从商店取回文件:
r.table("stores")
.concatMap(function (x) {
return x("locations").map(function (loc) {
return x.merge({locations: loc});
});
})
.eqJoin("locations", r.table("locations"))
Run Code Online (Sandbox Code Playgroud)