如何使用ID数组连接表 [英] How to join tables with a array of IDs
问题描述
尝试使用此示例连接ID数组: https:/ /github.com/rethinkdb/rethinkdb/issues/1533#issuecomment-26112118
Attempting to use this example to join on an array of IDs: 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"
]
}
地点表格摘录
{
"lat": 125.231345,
"lng": 44.23123,
"id": "80362c86-94cc-4be3-b2b0-2607901804dd"
}
我想选择商店并加入他们的商店位置。
I'd like to select the stores and join their store locations.
来自ReThinkDB撰稿人的原始示例:
Original example from ReThinkDB contributor:
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"))
我尝试转换为JS
r.table("stores")
.concatMap((function(x){
return x("locations").map((function(y){
return x("locations").add(y);
}))
}))
.eqJoin("locations", r.table("locations"))
结果
RqlRuntimeError:预期类型为ARRAY但发现STRING
推荐答案
您正在使用concatMap,这是您想要查询的第一部分。
You're using concatMap incorrectly, here's what you want the first part of your query to be.
r.table("stores")
.concatMap(function (x) {
return x("locations");
})
尝试运行它,它应该给你:
Try running that, it should give you:
["5fa96762-...", "80362c86-...", ...]
现在我们需要将此连接到另一个表。要将一个id数组连接到一个表,你可以像这样使用eqjoin:
Now we need to join this to the other table. To join an array of ids to a table you can use eqjoin like so:
array.eqJoin(function (row) { return row; }, table)
这里有更多细节: rql从javascript中的密钥rethinkdb列表中获取多个文档。
总结我们得到:
r.table("stores")
.concatMap(function (x) {
return x("locations")
})
.eqJoin(function (i) { return i; }, r.table("locations"))
To从商店取回文件:
To get back the documents from the stores:
r.table("stores")
.concatMap(function (x) {
return x("locations").map(function (loc) {
return x.merge({locations: loc});
});
})
.eqJoin("locations", r.table("locations"))
这篇关于如何使用ID数组连接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!