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 },
> 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
You should read it
May be interested
- Text Search in MongoDBstarting with version 2.4, mongodb started supporting text indexes to search within the string content.
- Shard in MongoDBsharding is a process of storing data records across multiple devices and it is a method of mongodb to meet the requirement for increasing data. when the size of the data increases, a single device cannot be enough to store data.
- Learn about Java Driver in MongoDBin the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
- Install MongoDBinstructions for installing mongodb on windows.
- Map Reduce in MongoDBin mongodb documentation, map-reduce is a data processing system that condenses a large amount of data into useful overall results. mongodb uses mapreduce command for map-reduce operation. in general, map reduce is used to handle large data sets.
- Data modeling in MongoDBdata in mongodb has a flexible schema. documents in the same collection need not have the same set of fields or structures, and common fields in collection documents can keep different data types.
- GridFS in MongoDBgridfs is the mongodb specification for storing and collecting large files such as images, audio, video files, etc. it is a type of file system to store files but its data is stored inside mongodb collections. .
- Query analysis in MongoDBanalyzing queries is a very important aspect to assess the effectiveness of database and the effectiveness of the designed index. we will explore the two most frequently used queries, $ explain and $ hint.
- Projection in MongoDBin mongodb, projection's meaning is to select only the necessary data instead of selecting the entire data of a document. if a document has 5 fields and you only need 3 fields, you should only select 3 fields from that document.
- Reference Database in MongoDBas shown in the relationship chapter in mongodb, to deploy a standardized database structure in mongodb, we use the referenced relationship concept, also known as manual references, in which we manipulate to store the id of the documents referenced in another document.