HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
MongoDB polymerized (aggregate) mainly for processing the data (such as statistical averages, sums, etc.), and returns the result of the calculated data. Somewhat similar sql statement count (*).
MongoDB polymerization method uses aggregate ().
The basic syntax aggregate () method is as follows:
>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
Data collection is as follows:
{ _id: ObjectId(7df78ad8902c) title: 'MongoDB Overview', description: 'MongoDB is no sql database', by_user: 'w3cschool.cc', url: 'https://w3resource.net', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }, { _id: ObjectId(7df78ad8902d) title: 'NoSQL Overview', description: 'No sql database is very fast', by_user: 'w3cschool.cc', url: 'https://w3resource.net', tags: ['mongodb', 'database', 'NoSQL'], likes: 10 }, { _id: ObjectId(7df78ad8902e) title: 'Neo4j Overview', description: 'Neo4j is no sql database', by_user: 'Neo4j', url: 'https://neo4j.com/', tags: ['neo4j', 'database', 'NoSQL'], likes: 750 },
Now we set the above is calculated for each of the number of articles written by authors using aggregate () calculated as follows:
> db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}]) { "result" : [ { "_id" : "w3cschool.cc", "num_tutorial" : 2 }, { "_id" : "Neo4j", "num_tutorial" : 1 } ], "ok" : 1 } >
Similar examples above sql statement: select by_user, count (*) from mycol group by by_user
In the above example, we by_user field by field to group the data and calculates the sum of the same value by_user field.
The following table shows some aggregation expression:
expression | description | Examples |
---|---|---|
$ Sum | Calculate the sum. | db.mycol.aggregate ([{$ group: {_id: "$ by_user", num_tutorial: {$ sum: "$ likes"}}}]) |
$ Avg | Calculating the average | db.mycol.aggregate ([{$ group: {_id: "$ by_user", num_tutorial: {$ avg: "$ likes"}}}]) |
$ Min | Gets a collection of all the documents correspond worth minimum. | db.mycol.aggregate ([{$ group: {_id: "$ by_user", num_tutorial: {$ min: "$ likes"}}}]) |
$ Max | Gets a collection of all documents corresponding to the maximum worth. | db.mycol.aggregate ([{$ group: {_id: "$ by_user", num_tutorial: {$ max: "$ likes"}}}]) |
$ Push | In the resulting document to insert a value into an array. | db.mycol.aggregate ([{$ group: {_id: "$ by_user", url: {$ push: "$ url"}}}]) |
$ AddToSet | In the resulting document to insert a value into an array, but does not create a copy. | db.mycol.aggregate ([{$ group: {_id: "$ by_user", url: {$ addToSet: "$ url"}}}]) |
$ First | Being the first document data according to the sort resource documents. | db.mycol.aggregate ([{$ group: {_id: "$ by_user", first_url: {$ first: "$ url"}}}]) |
$ Last | Gets the last document data according to the sort resource documents | db.mycol.aggregate ([{$ group: {_id: "$ by_user", last_url: {$ last: "$ url"}}}]) |
Pipes in Unix and Linux in general is used to output the current command as a parameter to the next command.
MongoDB MongoDB document polymeric pipe in the pipe after a processed result to the next pipeline processing. Pipeline operation can be repeated.
Expression: processing input and output documents. Expressions are stateless, can only be used to calculate the current pipeline of aggregate documents, you can not deal with other documents.
Here we introduce the aggregation framework commonly used in several operations:
1, $ project examples
db.article.aggregate( { $project : { title : 1 , author : 1 , }} );
In this case the results would only there _id, tilte and author of three fields, default _id field is to be included, if the order does not contain _id if it can be:
db.article.aggregate( { $project : { _id : 0 , title : 1 , author : 1 }});
2. $ match examples
db.articles.aggregate( [ { $match : { score : { $gt : 70, $lte : 90 } } }, { $group: { _id: null, count: { $sum: 1 } } } ] );
$ Match is used to obtain a score greater than 70 is less than or equal to 90 records, then the matching records to the next stage of the pipeline operator $ group for processing.
3. $ skip instance
db.article.aggregate( { $skip : 5 });
After a $ skip processing pipeline operator, the first five documents are "filtered" out.