Golang 中的聚合 mgo for Mongodb

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

有人知道我们在 mongodb shell 中为 golang mgo/bson 使用的聚合命令是什么吗?

Anybody knows what’s the equivalent of aggregate command we use in mongodb shell for 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"}}}])

推荐答案

假设 c 是你的 Collection:

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 参考资料:

GoDoc references:

  • Collection.Pipe 文档
  • 管道及其方法

更多推荐

Golang 中的聚合 mgo for Mongodb

本文发布于:2023-11-22 15:07:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617906.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Golang   mgo   Mongodb

发布评论

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

>www.elefans.com

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