如何在Golang的RethinkDB驱动程序gorethink中重命名结果字段?

huangapple go评论161阅读模式
英文:

How to rename result field in golang rethinkdb driver gorethink?

问题

以下是要翻译的内容:

r.table('customers')
  .map(function(purchase) {
      return {zip:customer('address')('zip'), product:purchase('name')};
    })      
 .run(...)

gorethink中,如何表示map({"new_column_name":Row.Field("original_column_name")})

非常感谢任何帮助...

英文:
r.table('customers')
  .map(function(purchase) {
      return {zip:customer('address')('zip'), product:purchase('name')};
    })      
 .run(...)

How can i represent map({"new_column_name":Row.Field("original_column_name")}) in
gorethink

Any help is appreciated...

答案1

得分: 2

我刚刚将你的Node.js代码(在你之前的版本中)转换成了Golang:

session, err := r.Connect(r.ConnectOpts{
  Address: "localhost:28015",
})
if err != nil {
  return
}

res, err := table.ConcatMap(func(customer r.Term) interface{} {
  return customer.Field("purchases").Map(func(purchase r.Term) interface{} {
    return map[string]interface{}{
      "zip":     customer.Field("address").Field("zip"),
      "product": purchase.Field("name"),
    }
  })
}).Run(session)

if err != nil {
  return
}

defer res.Close()

var response interface{}
for res.Next(&response) {
  fmt.Println(response)
}

以上是翻译好的代码部分。

英文:

I just port your node.js code (in your former version) to golang :

  session, err := r.Connect(r.ConnectOpts{
    Address: "localhost:28015",
  })
  if err != nil {
    return
  }

  res, err := table.ConcatMap(func(customer r.Term) interface{} {
    return customer.Field("purchases").Map(func(purchase r.Term) interface{} {
      return map[string]interface{}{
        "zip":     customer.Field("address").Field("zip"),
        "product": purchase.Field("name"),
      }
    })
  }).Run(session)

  if err != nil {
    return
  }

  defer res.Close()

  var response interface{}
  for res.Next(&response) {
    fmt.Println(response)
  }

huangapple
  • 本文由 发表于 2016年4月20日 13:39:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/36734721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定