Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregation in Golang mgo for Mongodb

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"}}}])
like image 872
Daemon1313 Avatar asked Oct 19 '14 23:10

Daemon1313


People also ask

Is aggregation good in MongoDB?

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.

Which aggregation method is preferred for use by MongoDB?

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.

How aggregation is performed in MongoDB?

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.

Does MongoDB have aggregation?

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.

What is aggregation in MongoDB and how it works?

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.

How to insert data in MongoDB using Golang?

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:

What is the aggregation pipeline in MongoDB?

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:

How to choose the page to disk method in MongoDB?

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.


2 Answers

Sample Code:

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)

Note:

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.

Output:

{
    "transactions": [
        {
            "_id": "[email protected]",
            "count": 10
        },
        {
            "_id": "[email protected]",
            "count": 12
        }
    ]
}
like image 127
George Livingston Avatar answered Oct 10 '22 07:10

George Livingston


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:

  • Collection.Pipe documentation
  • Pipe and its methods
like image 31
Jarema Avatar answered Oct 10 '22 08:10

Jarema