Jamie
Jamie

Reputation: 3051

Joining on array with RethinkDB

I'm having some issues with joining two tables.

I've two tables:

Qas

[{
  "course":  "swd" ,
  "id":  "c9b2e8cb-15f9-4f93-b677-6dff2880d383" ,
  "number": 1 ,
  "questions": [
    {
      "date":  "Wednesday, December 16th, 2015, 11:09" ,
      "owner": 4362445 ,
      "question":  "Could you explain promises?" ,
      "questionid":  "2766d4bf-fd79-4f94-8d22-788d4e6b89c2" ,
    }
] ,
"session": 1 ,
"unit": 1
}]

Users

[{
  "avatar_url": https://avatars.githubusercontent.com/u/4362445?v=3, »
  "displayName":  "Jamie" ,
  "id": 4362445 ,
  "oauth_token":  "46451371bffea867a71b9cc357eff4fd9a06591e" ,
  "role":  "student" ,
  "team": {
  "id": 1729535 ,
  "name":  "dwa-group-a"
} ,
"username":  "Jamiek94"
}]

Now i'd like to join (qas -> questions - > owner) on (users -> id) and finally zip them togheter. So the result would look like this:

[{
  "course":  "swd" ,
  "id":  "c9b2e8cb-15f9-4f93-b677-6dff2880d383" ,
  "number": 1 ,
  "questions": [
    {
      "date":  "Wednesday, December 16th, 2015, 11:09" ,
      "owner": 4362445 ,
      "question":  "Could you explain promises?" ,
      "questionid":  "2766d4bf-fd79-4f94-8d22-788d4e6b89c2" ,
      "user" : {
        "avatar_url": https://avatars.githubusercontent.com/u/4362445?v=3, »
        "displayName":  "Jamie" ,
        "id": 4362445 ,
        "oauth_token":  "46451371bffea867a71b9cc357eff4fd9a06591e" ,
        "role":  "student" ,
        "team": {
        "id": 1729535 ,
        "name":  "dwa-group-a"
     }
    }
] ,
"session": 1 ,
"unit": 1
}]

The query i am using is:

r.db('GitSmurf').table('qa').eqJoin(r.row('questions')('owner'), r.db('GitSmurf').table('users'), { index : 'id'})

This results in:

No results were returned for this query

Upvotes: 0

Views: 292

Answers (1)

kureikain
kureikain

Reputation: 2314

your questions is an array so you cannot use r.row('questions')('owner')

We can scratch eqJoin and just join manually with map. Something like this should work for you:

r.table('Qas').merge(function(qa) {
  return {
    questions: qa('questions').map(function(q) {
      return q.merge({user: r.table('Users').get(q('owner'))})
    })
  }
})

Upvotes: 1

Related Questions