【问题标题】:How to join tables with a array of IDs如何连接具有 ID 数组的表
【发布时间】:2014-01-03 17:50:20
【问题描述】:

尝试使用此示例加入 ID 数组:https://github.com/rethinkdb/rethinkdb/issues/1533#issuecomment-26112118

存储表 sn-p

{ 
  "storeID": "80362c86-94cc-4be3-b2b0-2607901804dd",
  "locations": [
    "5fa96762-f0a9-41f2-a6c1-1335185f193d",
    "80362c86-94cc-4be3-b2b0-2607901804dd"
  ]
}

位置表 sn-p

{
  "lat": 125.231345,
  "lng": 44.23123,
  "id": "80362c86-94cc-4be3-b2b0-2607901804dd"
}

我想选择商店并加入他们的商店位置。

来自 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"))

我尝试转换为 JS

r.table("stores")
 .concatMap((function(x){ 
   return x("locations").map((function(y){ 
     return x("locations").add(y);
   })) 
}))
.eqJoin("locations", r.table("locations"))

结果

RqlRuntimeError: Expected type ARRAY but found STRING

【问题讨论】:

    标签: javascript rethinkdb


    【解决方案1】:

    您使用的 concatMap 不正确,这就是您希望查询的第一部分的内容。

    r.table("stores")
     .concatMap(function (x) {
       return x("locations");
     })
    

    尝试运行它,它应该会给你:

    ["5fa96762-...", "80362c86-...", ...]
    

    现在我们需要将它连接到另一个表。要将 id 数组连接到表中,您可以像这样使用 eqjoin:

    array.eqJoin(function (row) { return row; }, table)
    

    这里有更多详细信息:rql get multple documents from list of keys rethinkdb in javascript

    把它们放在一起,我们得到:

    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"))
    

    【讨论】:

    • 惊人的,谢谢。跟进:如果我想返回我的商店表信息以及关联的位置表信息(而不仅仅是关联的位置信息),是否需要运行单独的查询?
    • 此查询不应仅返回位置信息。联接从两个表中返回数据。
    • 它只返回我的位置对象数组。左表只是ID(而不是所有store字段),右表是所有location字段。
    猜你喜欢
    • 2017-07-17
    • 2021-06-08
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多