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
?