go-在Go RethinkDB中合并

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?

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)

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"),
        },
    }
})