Rethinkdb合并结果

Rethink db merge results

我是 RethinkDB 的新手。加入后如何将正确的结果放入左侧 table 作为公司:[{right}, ...] 现在我使用这个查询 r.db('Auth').table('Users').filter({'email': '******'}).eqJoin('id', r.db('CloudCRM').table("Emploees"),{index: 'user_id'}).zip().without('user_id').eqJoin("company_id", r.db('Auth').table("Companies"), {index: 'id'}) 结果如下所示:

{
"left": {
"birthdate": ****,
"company_id":  "b191c180-0c51-4317-b545-2597a15d6ddb" ,
"email": ******, »
"firstName":  "****" ,
"id":  "c49c8712-8904-42ae-9819-4ab54b4d56ff" ,
"lastName":  "******" ,
"password":  "******" ,
"phonenumber":  "*****" ,
"role":  "user"
} ,
"right": {
"companyTitle":  "*******“" ,
"director":  "b2020c9e-4d01-41d8-82fa-c080bbeb5a68" ,
"domain":  "*******" ,
"id":  "b191c180-0c51-4317-b545-2597a15d6ddb"
}
}

我想得到的是:

{
"birthdate": ******,
"company_id":  "b191c180-0c51-4317-b545-2597a15d6ddb" ,
"email": *******, »
"firstName":  "*****" ,
"id":  "c49c8712-8904-42ae-9819-4ab54b4d56ff" ,
"lastName":  "******" ,
"password":  "******" ,
"phonenumber":  "******" ,
"role":  "user"
"companies": [{
"companyTitle":  "********" ,
"director":  "b2020c9e-4d01-41d8-82fa-c080bbeb5a68" ,
"domain":  "*******" ,
"id":  "b191c180-0c51-4317-b545-2597a15d6ddb"
}]
}

我就是这样解决的

r.db('Auth').table('Users').filter({'email': 'ignas.rackus@gmail.com'}).eqJoin('id', r.db('CloudCRM').table("Emploees"),{index: 'user_id'}).zip().without('user_id').eqJoin("company_id", r.db('Auth').table("Companies"), {index: 'id'})
  .hasFields('position_id').eqJoin("position_id", r.db('CloudCRM').table("Possitions"), {index: 'id'})
  .map({
  id : r.row('left')('id'),
  firstName : r.row('left')('firstName'),
  lastName : r.row('left')('lastName'),
  email : r.row('left')('email'),
  password : r.row('left')('password'),
  birthdate : r.row('left')('birthdate'),
  phonenumber : r.row('left')('phonenumber'),
  companies : r.row('right')
})

这可能不是最好的解决方案,所以如果有人知道更好的请指正我

您可以避免 mistake-prone 复制每个 属性 value-by-value,只需选择 left 值(用户)合并 right 值(公司)放入其中。例如(为简单起见,下面的查询更简单以便于理解),

r.db('test')
  .table('users')
  .eqJoin('company_id', r.table('companies'))
  .map((row) => row('left').merge({ company: row('right') }))
  ;

输出:

{
    "birthdate":  "****" ,
    "company": {
        "companyTitle":  "*******“" ,
        "director":  "b2020c9e-4d01-41d8-82fa-c080bbeb5a68" ,
        "domain":  "*******" ,
        "id":  "b191c180-0c51-4317-b545-2597a15d6ddb"
    } ,
    "company_id":  "b191c180-0c51-4317-b545-2597a15d6ddb" ,
    "email":  "******" ,
    "firstName":  "****" ,
    "id":  "c49c8712-8904-42ae-9819-4ab54b4d56ff" ,
    "lastName":  "******" ,
    "password":  "******" ,
    "phonenumber":  "*****" ,
    "role":  "user"
}

请注意,结果提供了一个 company 字段,因为您的用户 table 每个用户有一个 company_id