Gorang mgo for Mongodb的聚合

Dae*_*313 8 go mongodb aggregation-framework

有谁知道我们在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"}}}])
Run Code Online (Sandbox Code Playgroud)

Jar*_*ema 25

假设这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
Run Code Online (Sandbox Code Playgroud)

GoDoc参考: