如何在进行连接时重命名重新思考的字段

How to rename a field in rethink while doing a join

我正在尝试使用以下查询在 rethinkdb 中对 2 tables 进行连接:

r.db('testdb')
  .table('eco')
  .eqJoin('project_id', r.db('testdb').table('projects'))
  .map(
    function(){ 
      r.row.merge(function(){ 
        r.expr({'right': r.expr({'p_name': r.row['right']['name']})})
      })
    })
    .without(r.expr({'right': r.expr({'id': True})}))
    .without(r.expr({'right': r.expr({'name': True})}))
    .zip()
I keep getting the following error:

TypeError: 无法读取未定义的 属性 'name'

生态 table 以及项目 table 上有一个名称字段。

您需要在 select 字段中使用圆括号而不是方括号。

r.db('testdb')
  .table('eco')
  .eqJoin('project_id', r.db('testdb').table('projects'))
  .map(
    function(doc){ 
      return doc.merge(function(){ 
        return {'right': {'p_name': doc('right')('name')}}
      })
    })
    .without({'right': {'id': True}})
    .without({'right': {'name': True}})
    .zip()

站点注释:

  • 如果你使用一个函数,你不应该使用 r.row -- 它是一个或另一个,而不是两个。
  • 您不需要将所有内容都包装在 r.expr

我觉得这个符号更简洁一些:

r.db('testdb')
  .table('eco')
  .eqJoin('project_id', r.db('testdb').table('projects'))
  .map((doc) => ({
    left: doc('left'),
    right: { p_name: doc('right')('name') }
  }))
  .zip()

鉴于您只想从 projects table.

p_name