Monglang的Golang mgo中的聚合

编程入门 行业动态 更新时间:2024-10-23 09:39:46
本文介绍了Monglang的Golang mgo中的聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人知道我们在mongodb shell中用于golang mgo / bson的集合命令的等价物是什么?

类似的:

aggregate([{$ match:{my_id:ObjectId(543d171c5b2c1242fe0019)}},{$ sort:{my_id:1,dateInfo:1,name:1 }},{$ group:{_ id:$ my_id,lastEntry:{$ max:$ dateInfo},nm:{$ last:$ name}}}])

解决方案 :

pipe:= c.Pipe([] bson.M {{$ match:bson.M {name :John}}}) resp:= [] bson.M {} err:= pipe.All(& resp) if err!= nil { //处理错误} fmt.Println(resp)//简单打印证明它正在工作

GoDoc参考资料: Collection.Pipe文档

  • Pipe a nd其方法
  • Anybody knows what’s the equivalent of aggregate command we use in mongodb shell for golang mgo/bson?

    Something like that:

    aggregate([{$match:{my_id:ObjectId("543d171c5b2c1242fe0019")}},{$sort:{my_id:1, dateInfo:1, name:1}},{$group:{_id:"$my_id", lastEntry:{$max: "$dateInfo"},nm:{$last:"$name"}}}])

    解决方案

    Assuming that c is your Collection:

    pipe := c.Pipe([]bson.M{{"$match": bson.M{"name":"John"}}}) resp := []bson.M{} err := pipe.All(&resp) if err != nil { //handle error } fmt.Println(resp) // simple print proving it's working

    GoDoc references:

    • Collection.Pipe documentation
    • Pipe and its methods

    更多推荐

    Monglang的Golang mgo中的聚合

    本文发布于:2023-11-22 15:09:01,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:Monglang   Golang   mgo

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!