go – 在Go RethinkDB中合并

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

go - Merge in Go RethinkDB

问题

我一直在努力适应gorethink(Go语言中的RethinkDB驱动程序)中的Merge操作。

  1. result, err := r.Table("quote").GetAllByIndex("idUser", idUser).
  2. OrderBy(r.Desc("created_at")).Merge(func(row r.Term) interface{}{
  3. res, _ := r.Table("article").Get(row.Field("idArticle")).Run(session)
  4. // TODO
  5. return map[string]interface{}{
  6. // TODO
  7. }
  8. }).Run(session)

我有3个集合:articlequoteuser

通过上述函数,我打算实现以下功能:

  • 根据idUser查询所有quote文档
  • 对于每个获取到的quote,我想使用其对应的idArticle字段在article集合中进行查询
  • 在使用idArticle获取到相应文档后,我使用它来查询该文档的keywords字段
  • 最后,我将keywords数组合并到每个quote文档中

在RethinkDB的JavaScript API中,我是这样实现的:

  1. findAllByUser = function(idU){
  2. return r.table(table)
  3. .filter({idUser: idU})
  4. .orderBy(r.desc('created_at'))
  5. .merge(function(quote){
  6. var article = r.table('article').get(quote('idArticle'));
  7. return {
  8. tags: {keywords: article('keywords')}
  9. }
  10. })
  11. .run(connection)
  12. .then(function(result){
  13. return result.toArray();
  14. });
  15. }

但是我仍然没有成功在gorethink中实现相同的功能。我该如何获取Term的值row.Field("idArticle")并将其用于后续的查询和Merge操作?

英文:

I've been struggling to get used to Merge of gorethink (RethinkDB's driver in Go)

  1. result,err:=r.Table("quote").GetAllByIndex("idUser",idUser).
  2. OrderBy(r.Desc("created_at")).Merge(func(row r.Term) interface{}{
  3. res,_:=r.Table("article").Get(row.Field("idArticle")).Run(session)
  4. // TODO
  5. return map[string]interface{}{
  6. // TODO
  7. }
  8. }).Run(session)

I have 3 collections: article, quote, and user

With the above function I intend to:

  • Query all quote documents by idUser
  • With each quote I get, I want to use its respective idArticle field to query in article collection
  • After getting the appropriate document using idArticle, I use it to query that document's keywords fields
  • Finally, I merge keywords array to each quote document

In JavaScript API for RethinkDB, I implemented like this:

  1. findAllByUser = function(idU){
  2. return r.table(table)
  3. .filter({idUser: idU})
  4. .orderBy(r.desc('created_at'))
  5. .merge(function(quote){
  6. var article = r.table('article').get(quote('idArticle'));
  7. return {
  8. tags: {keywords: article('keywords')}
  9. }
  10. })
  11. .run(connection)
  12. .then(function(result){
  13. return result.toArray();
  14. });
  15. }

But I still haven't managed to do the same in gorethink. How can I get the value of Term row.Field("idArticle") and use it for later queries and Merge?

答案1

得分: 1

你可以通过使用子查询而不调用Run来将你的JS查询转换为Go。例如:

  1. r.Table(table).
  2. Filter(map[string]interface{}{"idUser": idU}).
  3. OrderBy(r.Desc("created_at")).
  4. Merge(func(quote r.Term) {
  5. article := r.Table("article").Get(quote.Field("idArticle"))
  6. return map[string]interface{}{
  7. "tags": map[string]interface{}{
  8. "keywords": article("keywords"),
  9. },
  10. }
  11. })

请注意,这只是一个示例,具体的转换可能需要根据你的代码和需求进行调整。

英文:

Copied my answer from https://github.com/dancannon/gorethink/issues/291

You should be able to convert your JS query to Go by using the subquery without calling Run. For example:

  1. r.Table(table)
  2. .Filter(map[string]interface{}{"idUser": idU})
  3. .OrderBy(r.Desc("created_at"))
  4. .Merge(func(quote r.Term){
  5. article := r.Table("article").Get(quote.Field("idArticle"))
  6. return map[string]interface{}{
  7. "tags": map[string]interface{}{
  8. "keywords": article("keywords"),
  9. },
  10. }
  11. })

答案2

得分: 0

非常感谢。我成功解决了恐慌错误。

正确的答案是:

  1. result, err := r.Table("quote").Filter(r.Row.Field("idUser").Eq(idUser)).
  2. OrderBy(r.Desc("created_at")).
  3. Merge(func(quote r.Term) interface{} {
  4. fmt.Println(quote.Field("idArticle"))
  5. article := r.Table("article").Get(quote.Field("idArticle"))
  6. return map[string]interface{}{
  7. "tags": map[string]interface{}{
  8. "keywords": article.Field("keywords"),
  9. },
  10. }
  11. }).
  12. Run(config.Connection())
  13. 我因为昨晚返回的游标结果地址声明而感到恐慌
  14. 它是
  15. `var all map[string]interface{}`
  16. 而应该是
  17. `var all []interface{}`
  18. 然后在`err = result.All(&all)`中发生了恐慌
英文:

Thank you very much. I managed to solve the panicking error

True answer is:

  1. result,err:=r.Table("quote").Filter(r.Row.Field("idUser").Eq(idUser)).
  2. OrderBy(r.Desc("created_at")).
  3. Merge(func(quote r.Term) interface{}{
  4. fmt.Println(quote.Field("idArticle"))
  5. article:=r.Table("article").Get(quote.Field("idArticle"))
  6. return map[string]interface{}{
  7. "tags":map[string]interface{}{
  8. "keywords":article.Field("keywords"),
  9. },
  10. }
  11. }).
  12. Run(config.Connection())

I got panicked because of last night's statement of returned address for cursor result:
It was:
var all map[string]interface{}
While it should be:
var all []interface{}
Then it panicked in err=result.All(&all)

huangapple
  • 本文由 发表于 2016年3月29日 22:29:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/36287265.html
匿名

发表评论

匿名网友

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

确定