javascript - 如何连接具有 ID 数组的表

标签 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"
  ]
}

位置表片段

{
  "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:期望类型 ARRAY 但找到了 STRING

最佳答案

您使用的 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"))

关于javascript - 如何连接具有 ID 数组的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20909723/

相关文章:

javascript - 我应该将通用端点的算法放在 Swaggerize-Restify 项目中的哪里?

javascript - 使用 Javascript 连接到 XMPP 服务器

node.js - RethinkDB 与 NodeJS : Open individual connections vs single static connection

javascript - 如何在 Three.js 编辑器启动时加载 CTM 文件?

javascript - 通过 Ajax 上传照片后替换 css 背景图像

javascript - 使用 socket.io 进行实时搜索

nosql - Rethinkdb 从文档中删除数据

rethinkdb - rethinkdb 中的链接查询

javascript - 谷歌地图——设置默认位置,这样你就不必为每个路由请求输入城市、州

rethinkdb - 在 RethinkDB 中使用 'replace' 与 'update' 冲突解决有什么功能差异