Javascript 如何使用ID数组连接表
正在尝试使用此示例加入ID数组: 存储表代码段 位置表代码段 我想选择店铺并加入他们的店铺位置。 来自RejectDB contributor的原始示例:Javascript 如何使用ID数组连接表,javascript,rethinkdb,Javascript,Rethinkdb,正在尝试使用此示例加入ID数组: 存储表代码段 位置表代码段 我想选择店铺并加入他们的店铺位置。 来自RejectDB 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
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:应为类型数组,但找到字符串
您不正确地使用了concatMap,下面是您希望查询的第一部分是什么
r.table("stores")
.concatMap(function (x) {
return x("locations");
})
尝试运行它,它将为您提供:
["5fa96762-...", "80362c86-...", ...]
现在我们需要将这个表连接到另一个表。要将ID数组连接到表,可以使用eqjoin,如下所示:
array.eqJoin(function (row) { return row; }, table)
此处有更多详细信息:
综合起来,我们得到:
r.table("stores")
.concatMap(function (x) {
return x("locations")
})
.eqJoin(function (i) { return i; }, r.table("locations"))
要从商店取回文档,请执行以下操作:
r.table("stores")
.concatMap(function (x) {
return x("locations").map(function (loc) {
return x.merge({locations: loc});
});
})
.eqJoin("locations", r.table("locations"))
非常好,谢谢。跟进:如果我想返回我的存储表信息,加上关联的位置表信息(而不仅仅是关联的位置信息),我是否需要运行单独的查询?此查询不应该只返回位置信息。联接两个表的返回数据。它只返回一个my location对象数组。左表只是ID(而不是所有存储字段),右表是所有位置字段。
r.table("stores")
.concatMap(function (x) {
return x("locations")
})
.eqJoin(function (i) { return i; }, r.table("locations"))
r.table("stores")
.concatMap(function (x) {
return x("locations").map(function (loc) {
return x.merge({locations: loc});
});
})
.eqJoin("locations", r.table("locations"))