Reputation: 279
Anybody knows what’s the equivalent of aggregate command we use in mongodb shell for golang mgo/bson?
Something like that:
aggregate([{$match:{my_id:ObjectId("543d171c5b2c1242fe0019")}},{$sort:{my_id:1, dateInfo:1, name:1}},{$group:{_id:"$my_id", lastEntry:{$max: "$dateInfo"},nm:{$last:"$name"}}}])
Upvotes: 13
Views: 22060
Reputation: 3022
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)
Please note that the line should end with (,) if you are not breaking in (,) it will throw error message even if your query is correct.
{
"transactions": [
{
"_id": "[email protected]",
"count": 10
},
{
"_id": "[email protected]",
"count": 12
}
]
}
Upvotes: 2
Reputation: 3586
Assuming that c
is your Collection:
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 references:
Upvotes: 27