本文介绍了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 workingGoDoc 参考资料:
GoDoc references:
- Collection.Pipe 文档
- 管道及其方法
更多推荐
Golang 中的聚合 mgo for Mongodb
发布评论