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"}}}])
MongoDB Aggregation goes further though and can also perform relational-like joins, reshape documents, create new and update existing collections, and so on. While there are other methods of obtaining aggregate data in MongoDB, the aggregation framework is the recommended approach for most work.
The pipeline provides efficient data aggregation using native operations within MongoDB, and is the preferred method for data aggregation in MongoDB. The aggregation pipeline can operate on a sharded collection. The aggregation pipeline can use indexes to improve its performance during some of its stages.
In MongoDB, aggregation operations process the data records/documents and return computed results. It collects values from various documents and groups them together and then performs different types of operations on that grouped data like sum, average, minimum, maximum, etc to return a computed result.
Starting in MongoDB 4.2, you can update documents with an aggregation pipeline if you use the stages shown in Updates with Aggregation Pipeline. Aggregation pipelines run with the db. collection. aggregate() method do not modify documents in a collection, unless the pipeline contains a $merge or $out stage.
It collects values from various documents and groups them together and then performs different types of operations on that grouped data like sum, average, minimum, maximum, etc to return a computed result. It is similar to the aggregate function of SQL. In MongoDB, the aggregation pipeline consists of stages and each stage transforms the document.
GoLang MongoDB Inserting Record To insert data we first declare data struct that can be used to provide the structure of the data we are going to use. Then we can use the data in a structured manner to store it:
In the aggregation pipeline, you list out a series of instructions in a "stage." For each stage that's defined, MongoDB executes them one after another in order to give a finalized output you're able to use. Let's look at an example usage of the aggregate command:
To choose the page to disk method, you just need to use the option allowDiskUse, in this way: The documents returned by the aggregation query, either as a cursor or stored via $out () in another collection, are limited to 16MB.
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
}
]
}
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With