MongoDB Advanced Operations

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.