go - 在 Go 中合并 RethinkDB
go - Merge in Go RethinkDB
我一直在努力适应 Merge
of 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 获得适当的文档后,我用它来查询该文档的关键字字段
- 最后,我将关键字数组合并到每个引用文档
在 JavaScript API 中,对于 RethinkDB,我是这样实现的:
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"),
},
}
})
非常感谢。我设法解决了恐慌错误
正确答案是:
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)
中惊慌失措
我一直在努力适应 Merge
of 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 获得适当的文档后,我用它来查询该文档的关键字字段
- 最后,我将关键字数组合并到每个引用文档
在 JavaScript API 中,对于 RethinkDB,我是这样实现的:
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"),
},
}
})
非常感谢。我设法解决了恐慌错误
正确答案是:
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)