go – 在Go RethinkDB中合并

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

go - Merge in Go RethinkDB

问题

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

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

我有3个集合:articlequoteuser

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

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

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

findAllByUser = function(idU){
    return r.table(table)
    .filter({idUser: idU})
    .orderBy(r.desc('created_at'))
    .merge(function(quote){
        var article = r.table('article').get(quote('idArticle'));
        return {
            tags: {keywords: article('keywords')}
        }
    })
    .run(connection)
    .then(function(result){
        return result.toArray();
    });
}

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

英文:

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

result,err:=r.Table("quote").GetAllByIndex("idUser",idUser).
        OrderBy(r.Desc("created_at")).Merge(func(row r.Term) interface{}{
            res,_:=r.Table("article").Get(row.Field("idArticle")).Run(session)
            // TODO
            return map[string]interface{}{
                // TODO
            }
        }).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:

findAllByUser = function(idU){
        return r.table(table)
        .filter({idUser: idU})
        .orderBy(r.desc('created_at'))
        .merge(function(quote){
            var article = r.table('article').get(quote('idArticle'));
            return {
                tags: {keywords: article('keywords')}
            }
        })
        .run(connection)
        .then(function(result){
            return result.toArray();
        });
    }

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。例如:

r.Table(table).
    Filter(map[string]interface{}{"idUser": idU}).
    OrderBy(r.Desc("created_at")).
    Merge(func(quote r.Term) {
        article := r.Table("article").Get(quote.Field("idArticle"))
        return map[string]interface{}{
            "tags": map[string]interface{}{
                "keywords": article("keywords"),
            },
        }
    })

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

英文:

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:

r.Table(table)
.Filter(map[string]interface{}{"idUser": idU})
.OrderBy(r.Desc("created_at"))
.Merge(func(quote r.Term){
    article := r.Table("article").Get(quote.Field("idArticle"))
    return map[string]interface{}{
        "tags": map[string]interface{}{
            "keywords": article("keywords"),
        },
    }
})

答案2

得分: 0

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

正确的答案是:

result, err := r.Table("quote").Filter(r.Row.Field("idUser").Eq(idUser)).
    OrderBy(r.Desc("created_at")).
    Merge(func(quote r.Term) interface{} {
        fmt.Println(quote.Field("idArticle"))
        article := r.Table("article").Get(quote.Field("idArticle"))
        return map[string]interface{}{
            "tags": map[string]interface{}{
                "keywords": article.Field("keywords"),
            },
        }
    }).
    Run(config.Connection())

我因为昨晚返回的游标结果地址声明而感到恐慌
它是
`var all map[string]interface{}`
而应该是
`var all []interface{}`
然后在`err = result.All(&all)`中发生了恐慌
英文:

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

True answer is:

result,err:=r.Table("quote").Filter(r.Row.Field("idUser").Eq(idUser)).
		OrderBy(r.Desc("created_at")).
		Merge(func(quote r.Term) interface{}{
			fmt.Println(quote.Field("idArticle"))
			article:=r.Table("article").Get(quote.Field("idArticle"))
			return map[string]interface{}{
				"tags":map[string]interface{}{
					"keywords":article.Field("keywords"),
				},
			}
		}).
		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:

确定