我在重新思考db中有两个文件,如下面的
角色:
{"Name": "client" ,"id": "dfd1a03d-ddb2-458e-8379-f443daa0fcfd"}
{"Name": "admin" ,"id": "c1ed66a4-8e72-416b-8237-c4a0518826c8"
}
用户:
{"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"
}
我希望合并这两个表记录并显示如下
{"id": "ae9aaaea-24b6-4880-9a81-d6ee34c82dde" ,
"password": "$2a$10$MqaQjif6bigsoYV82s/SXeWsQQ9.NnEPykw1L/nLcfOmWRryq8XJe" ,
"roles": ["admin" ,"client"] ,"userName": "james"}
答案 0 :(得分:1)
您的问题有点不清楚,但如果我理解正确的话:
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')
})
})
答案 1 :(得分:0)
只是另一种解决方案:
r.table('users').merge({
roles: r.row('roles').map(function(role) {
return r.table('roles').get(role)('Name')
})
})
或以较短的方式重写Kludge的解决方案:
r.table('users').merge({
roles: r.table('roles').getAll(r.args(r.row('roles')))('Name').coerceTo('ARRAY')
})