Aggregation in MongoDB

Aggregation can be understood as aggregation. The Aggregation operation handles data records and returns calculated results. The operations group the values ​​from multiple documents together, and can perform multiple operations on the grouped data to return a single result. In SQL, count (*) and GROUP BY are equivalent to Aggregation in MongoDB.

Aggregation can be understood as aggregation. The Aggregation operation handles data records and returns calculated results. The operations group the values ​​from multiple documents together, and can perform multiple operations on the grouped data to return a single result. In SQL, count (*) and GROUP BY are equivalent to Aggregation in MongoDB.

Aggregate () method in MongoDB

With Aggregation in MongoDB, you should use the aggregate () method.

Syntax

The basic syntax of aggregate () method is as follows:

 > db . COLLECTION_NAME . aggregate ( AGGREGATE_OPERATION ) 

For example

In Collection, you have the following data:

 { _id : ObjectId ( 7df78ad8902c ) title : 'MongoDB Overview' , description : 'MongoDB is no sql database' , by_user : 'tutorials point' , url : 'http://www.tutorialspoint.com' , tags : [ 'mongodb' , 'database' , 'NoSQL' ], likes : 100 }, { _id : ObjectId ( 7df78ad8902d ) title : 'NoSQL Overview' , description : 'No sql database is very fast' , by_user : 'tutorials point' , url : 'http://www.tutorialspoint.com' , tags : [ 'mongodb' , 'database' , 'NoSQL' ], likes : 10 }, { _id : ObjectId ( 7df78ad8902e ) title : 'Neo4j Overview' , description : 'Neo4j is no sql database' , by_user : 'Neo4j' , url : 'http://www.neo4j.com' , tags : [ 'neo4j' , 'database' , 'NoSQL' ], likes : 750 }, 
From the Collection above, if you want to display a list of how many tutorials written by each user, you will use the aggregate () method as follows:
 > db . mycol . aggregate ([{ $group : { _id : "$by_user" , num_tutorial : { $sum : 1 }}}]) { "result" : [ { "_id" : "tutorials point" , "num_tutorial" : 2 }, { "_id" : "Neo4j" , "num_tutorial" : 1 } ], "ok" : 1 } > 

The equivalent SQL query for the above case is select by_user, count (*) from mycol group by by_user.

In the above example, we have grouped the Documents by the by_user field and on each occurrence of by_user, the previous value of the total will be increased. There is a list of available Aggregation expressions, listed below:

Expression Description Description $ sum The total value is determined from all documents in that Collection db.mycol.aggregate ([{$ group: {_id: "$ by_user", num_tutorial: {$ sum: "$ likes"}} }]) $ avg Calculate the average of all values ​​given from all documents in that Collection db.mycol.aggregate ([{$ group: {_id: "$ by_user", num_tutorial: {$ avg: "$ likes "}}}]) $ min Get the smallest value of values ​​from all documents in that Collection db.mycol.aggregate ([{$ group: {_id:" $ by_user ", num_tutorial: {$ min:" $ likes "}}}]) $ max Get the maximum value of values ​​from all documents in that Collection db.mycol.aggregate ([{$ group: {_id:" $ by_user ", num_tutorial: {$ max : "$ likes"}}}]) $ push Insert values ​​into an array in the resulting document db.mycol.aggregate ([{$ group: {_id: "$ by_user", url: {$ push: "$ url "}}}]) $ addToSet Insert value to an array in Document ending She, but does not create db.mycol.aggregate copies ([{$ group: {_id: "$ by_user", url: {$ addToSet: "$ url"}}}]) $ first Get the first Document from Source Document in db.mycol.aggregate group ([{$ group: {_id: "$ by_user", first_url: {$ first: "$ url"}}}]) $ last Get the last Document from Source Document according to db group. mycol.aggregate ([{$ group: {_id: "$ by_user", last_url: {$ last: "$ url"}}}])

Pipeline concept in MongoDB

In UNIX, the shell pipeline command means the ability to execute an operation on some input and use the output as the next input for the command, and . MongoDB also supports the same pipeline concept in Aggregation Framework. There is a set of possible stages and each stage takes a collection of Documents as an input and creates a set of resulting Documents (or the final JSON Document result at the end of the pipeline). This result, then, will be used again for the next stage .

The possible stages in Aggregation Framework are:

$ project : Used to select specific fields from a Collection.

$ match : This is a filtering operation and so it can reduce the number of Document provided as input for the next stage.

$ group : Perform real Aggregation, as shown above.

$ sort : Arrange Documents.

$ skip : Jump over the number of documents provided.

$ limit : Limit the number of documents.

$ unwind : Used to split a Document using an array into multiple Documents. Using this operation will create a number of documents for the next step.

According to Tutorialspoint

Previous article: Index (Index) in MongoDB

Next lesson: Replica Set in MongoDB

4 ★ | 1 Vote | 👨 280 Views
« PREV POST
NEXT POST »