Golang 中的聚合 mgo for Mongodb [英] Aggregation in Golang mgo for Mongodb

查看:29
本文介绍了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:

这篇关于Golang 中的聚合 mgo for Mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆