dqfaom241360 2016-03-29 14:29
浏览 25
已采纳

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?

  • 写回答

2条回答 默认 最新

  • donglu953744 2016-03-30 03:23
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)