Mobilpadde
Mobilpadde

Reputation: 1881

Join on multiple ids from the same table

I'm trying to join a two tables (versus and words) on multiple ids. I don't really know how to explain it, so I'm just going to show what I mean.

An excerpt from the versus-table:

{
    "date": 1427675857789,
    "hero":  "7b88a237-c288-48f1-bf45-2dcd9f812b54",
    "id":  "017fe06a-e37d-4f23-92a3-bc52b38de4d7",
    "nemesis":  "e87a6252-6d08-4c5a-b057-2718e8c07d93",
    "points": {
        "hero": 58659,
        "nemesis": 3021
    }
}

Excerpt from the words-table:

{
    "id":  "7b88a237-c288-48f1-bf45-2dcd9f812b54" ,
    "word":  "i"
},
{
    "id":  "e87a6252-6d08-4c5a-b057-2718e8c07d93" ,
    "word":  "the"
}

I'd like to join the two tables so get something like this:

{
    "date": 1427675857789,
    "hero": "i",
    "nemesis": "the",
    "points": {
        "hero": 58659,
        "nemesis": 3021
    }
}

This is what I have so far: r.table("versus").eqJoin("hero", r.table("words")).zip(), which gets me this:

{
    "date": 1427675857789 ,
    "hero":  "7b88a237-c288-48f1-bf45-2dcd9f812b54" ,
    "id":  "7b88a237-c288-48f1-bf45-2dcd9f812b54" ,
    "nemesis":  "e87a6252-6d08-4c5a-b057-2718e8c07d93" ,
    "points": {
        "hero": 60507 ,
        "nemesis": 3504
    } ,
    "word":  "i"
}

I'm a little puzzled about how I can join it on the hero-row as well as nemesis-row.

Though I'd be happy with any result that shows most of the things from the versus-table (Doesn't matter if id is there) and two the two words which corresponds to the hero and nemesis id.

EDIT: I've figured something out, but now I'm only able to get the first document, which kind of defeats the purpose of what I'm trying to do... Here's what I got: r.table("versus").eqJoin("hero", r.table("words")).zip().map(r.row.merge({hero: r.row("word")})).eqJoin("nemesis", r.table("words")).zip().map(r.row.merge({nemesis: r.row("word")})).without(["word", "id"])

Upvotes: 2

Views: 168

Answers (1)

Mobilpadde
Mobilpadde

Reputation: 1881

Well, I did it...finally!

If someone's interested, this is what my new ReQL looks like:

r.table("versus").concatMap(function(v){
  return r.table("words").getAll(v("hero"), {index: "id"}).map(function(w){
    return r.branch(
      v("hero").eq(w("id")),
      v.merge({hero: w("word")}),
      v
    )
  })
}).concatMap(function(v){
  return r.table("words").getAll(v("nemesis"), {index: "id"}).map(function(w){
    return r.branch(
      v("nemesis").eq(w("id")),
      v.merge({nemesis: w("word")}),
      v
    )
  })
}).without("id")

Upvotes: 2

Related Questions