猿问

Mongodb 的 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"}}}])


慕运维8079593
浏览 207回答 2
2回答

月关宝盒

假设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

动漫人物

示例代码:pipe := c.Pipe([]bson.M{bson.M{"$match": bson.M{"type": "stamp"}},        bson.M{"$group": bson.M{"_id": "$userid",            "count": bson.M{"$sum": "$noofsr"}}}})resp := []bson.M{}iter := pipe.Iter()err = iter.All(&resp)注意:请注意,该行应与(,)如果你不打破(,),它会抛出错误消息,即使您的查询是正确的结束。输出:{    "transactions": [        {            "_id": "three@four.com",            "count": 10        },        {            "_id": "one@two.com",            "count": 12        }    ]}
随时随地看视频慕课网APP

相关分类

Go
我要回答