我一直在努力适应 Merge
的 gorethink
(RethinkDB 在 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)
我有 3 个集合:article
、quote
和 user
有了上面的功能我打算:
- 通过idUser查询所有报价文件
- 对于我得到的每个引用,我想使用其各自的 idArticle 字段在文章集合中进行查询
- 在使用 idArticle 获得适当的文档后,我用它来查询该文档的关键字字段
- 最后,我将关键字数组合并到每个报价文档
在 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
?
最佳答案
从 https://github.com/dancannon/gorethink/issues/291 复制了我的答案
您应该能够通过使用子查询将您的 JS 查询转换为 Go,而无需调用 Run
。例如:
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"),
},
}
})
关于javascript - go - 在 Go 中合并 RethinkDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36287265/