How to join tables with a array of IDs

微笑、不失礼 提交于 2019-11-30 13:10:50
Joe Doliner

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-...", ...]

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)

There's more details here: rql get multple documents from list of keys rethinkdb in javascript.

Putting it all together we get:

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

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"))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!