Polymerization aggregate
- Aggregation (aggregate) is primarily used to calculate data, similar to sum (), AVG () in SQL
- Grammar
db.集合名称.aggregate([{管道:{表达式}}])
Pipeline
- Pipelines are typically used in UNIX and Linux to input the output of the current command as the next command
ps ajx | grep mongo
- In MongoDB, pipelines have the same effect, and after the document is processed, the next processing is done through the pipeline
- Common piping
- $group: Grouping documents in a collection for statistical results
- $match: Filter data to only output documents that match the criteria
- $project: Modify the structure of the input document, such as renaming, adding, deleting fields, creating calculation results
- $sort: Sort the input document after output
- $limit: Limit the number of documents returned by the aggregation pipeline
- $skip: Skips a specified number of documents and returns the remaining documents
- $unwind: Splitting a field of an array type
An expression
- Process the input document and output
- Grammar
表达式:‘$列名‘
- Common expressions
- $sum: Calculate sum, $sum: 1 count with Count
- $avg: Calculate average
- $min: Get the minimum value
- $max: Get maximum value
- $push: Inserting values into an array in the resulting document
- $first: Get the first document data based on the ordering of the resource documents
- $last: Get the last document data based on the ordering of the resource documents
$group
- Group documents in a collection for statistical results
- _ID represents the basis for grouping, using a field in the format ' $ field '
- Example 1: Statistics on the total number of boys and girls
db.stu.aggregate([ {$group: { _id:‘$gender‘, counter:{$sum:1} } }])
Group by null
- Divides all the documents in the collection into a group
- Example 2: Total number of students, average age
db.stu.aggregate([ {$group: { _id:null, counter:{$sum:1}, avgAge:{$avg:‘$age‘} } }])
Pivot data
- Example 3: Statistics on students ' gender and student names
db.stu.aggregate([ {$group: { _id:‘$gender‘, name:{$push:‘$name‘} } }])
- Use the $ $ROOT to add the contents of the document to the array of result sets, as shown in the following code
db.stu.aggregate([ {$group: { _id:‘$gender‘, name:{$push:‘$$ROOT‘} } }])
$group
- Group documents in a collection for statistical results
- _ID represents the basis for grouping, using a field in the format ' $ field '
- Example 1: Statistics on the total number of boys and girls
db.stu.aggregate([ {$group: { _id:‘$gender‘, counter:{$sum:1} } }])
Group by null
- Divides all the documents in the collection into a group
- Example 2: Total number of students, average age
db.stu.aggregate([ {$group: { _id:null, counter:{$sum:1}, avgAge:{$avg:‘$age‘} } }])
Pivot data
- Example 3: Statistics on students ' gender and student names
db.stu.aggregate([ {$group: { _id:‘$gender‘, name:{$push:‘$name‘} } }])
- Use the $ $ROOT to add the contents of the document to the array of result sets, as shown in the following code
db.stu.aggregate([ {$group: { _id:‘$gender‘, name:{$push:‘$$ROOT‘} } }])
MongoDB Advanced Operations