Muhammad Habib
Muhammad Habib

Reputation: 111

merging two table records from rethink db

I have two documents in rethink db like below

Roles:

{"Name":  "client" ,"id":  "dfd1a03d-ddb2-458e-8379-f443daa0fcfd"} 
{"Name":  "admin" ,"id":  "c1ed66a4-8e72-416b-8237-c4a0518826c8"
}

Users:

{"id":  "ae9aaaea-24b6-4880-9a81-d6ee34c82dde" ,
"password":  "$2a$10$MqaQjif6bigsoYV82s/SXeWsQQ9.NnEPykw1L/nLcfOmWRryq8XJe" ,
"roles": ["c1ed66a4-8e72-416b-8237-c4a0518826c8" ,"dfd1a03d-ddb2-458e-8379-f443daa0fcfd"
] ,
"userName":  "jbadly"
}

and i want to merge these two table records and display like below

{"id":  "ae9aaaea-24b6-4880-9a81-d6ee34c82dde" ,
"password":  "$2a$10$MqaQjif6bigsoYV82s/SXeWsQQ9.NnEPykw1L/nLcfOmWRryq8XJe" ,
"roles": ["admin" ,"client"] ,"userName":  "james"}

Upvotes: 1

Views: 277

Answers (2)

kureikain
kureikain

Reputation: 2314

Just another solution:

r.table('users').merge({
    roles: r.row('roles').map(function(role) {
      return r.table('roles').get(role)('Name')
    })
  })

Or rewrite Kludge's solution in a shorter way:

r.table('users').merge({
    roles: r.table('roles').getAll(r.args(r.row('roles')))('Name').coerceTo('ARRAY')
  })

Upvotes: 0

Kludge
Kludge

Reputation: 2835

Your question is a bit unclear, but if I understand you correctly:

r.db('my_db').table('users')
  .map(function(row) {
    return row.merge({
      roles: r.db('my_db').table('roles')
        .getAll(r.args(row('roles')))
        .coerceTo('array')
    })
  })

Upvotes: 1

Related Questions