2014-10-19 61 views
3

任何人都知道我们在mongodb shell中为golang mgo/bson使用的聚合命令的等价物是什么?Monglangb在Golang mgo中的聚合

类似的东西:

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

我现在无法写出完整的答案,但可以使用mgo的Pipe函数,请参阅http://godoc.org/labix.org/v2/mgo#Collection.Pipe – fmpwizard 2014-10-20 01:03:37

回答

16

假设c是您的收藏:

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引用: