Monglang的Golang mgo中的聚合 [英] Aggregation in Golang mgo for Mongodb
本文介绍了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}}}])
$ c假设 c
是您的收藏集,您可以使用
解决方案 :
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文档
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:
这篇关于Monglang的Golang mgo中的聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文