有人知道我们在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"}}}])
答案 0 :(得分: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
GoDoc参考:
答案 1 :(得分:0)
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
}
]
}