有人知道我们在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 { //handle error } fmt.Println(resp) // simple print proving it's working
GoDoc参考: