MongoDB Aggregations and pipelines

Source: Internet
Author: User

MongoDB Aggregation

MongoDB Aggregation (aggregate) is primarily used to process data (such as statistical averages, sums, etc.) and returns the computed data results.

Aggregate () Method

The method of aggregation in MongoDB uses aggregate ().

Grammar

The basic syntax format for the aggregate () method is as follows:

>db. Collection_name.aggregate (aggregate_operation)

Note: The parameter aggregate_operation can be an object (single processing) or an array of multiple objects (pipeline processing).

> Db.person.find ()

{"_id": ObjectId ("592ffd872108e8e79ea902b0"), "name": "ZJF", "Age": 30}

{"_id": ObjectId ("593011C8A92497992CDFAC10"), "name": "Xhj", "Age": 30}

{"_id": ObjectId ("59301270a92497992cdfac11"), "name": "Zzj", "Age": 2}

{"_id": ObjectId ("593015fda92497992cdfac12"), ' name ': ' My second child ', ' age ': ' I do not know '}

$group the name of the column to be returned in the contents of the grouped grouping is the name of the first column _id is not able to change the column after which it is grouped is a summary column can be arbitrarily clearly $sum is summarized by the way 1 is the summary of the contents can be columns such as ' $age ' can also be a value here sum (1) is C The Meaning of Ount (*).

> db.person.aggregate ({$group: {_id: ' $age ', Count: {$sum: 1}}})

{"_id": "I do not know", "Count": 1}

{"_id": 2, "Count": 1}

{"_id": +, "Count": 2}

$group: Groups The documents in the collection to be used for statistical results $group First group the data according to key.

$group syntax: {$group: {_id: <expression>, <field1>: {<accumulator1>: <expression1>}, ...}}

_ID is the key to be grouped

$group: The data that can be grouped performs the following expression calculations:

$sum: Calculates the sum.

$avg: Calculates the average.

$min: Gets the minimum value for all documents in the collection according to the grouping.

$max: Gets the maximum value for all documents in the collection according to the grouping.

$push: Adds the value of the specified expression to an array.

$addToSet: Adds the value of an expression to a collection (no duplicate values).

$first: Returns the first document for each group, if one is sorted, by sort, if not by default in the stored order.

$last: Returns the last document for each group, if one is sorted, by sort, if not by default in the stored order.

Limitations of pipeline operations:

Each stage of the pipeline is limited to 100MB of memory. If a node pipeline exceeds this limit, MongoDB will produce an error. In order to be able to handle large datasets, you can set Allowdiskuse to true to write data to temporary files on the aggregation pipeline node. This will resolve the 100MB memory limit.

This limit may not be the latest version, if used, you can go to https://docs.mongodb.com/to check the official manual.

The concept of piping

Pipelines are typically used in UNIX and Linux to use the output of the current command as a parameter to the next command.

MongoDB's aggregation pipeline passes the MongoDB document to the next pipeline processing after a pipeline has finished processing. Pipeline operations can be repeated.

Expression: Processes the input document and outputs it. The expression is stateless and can only be used to calculate the document for the current aggregated pipeline and cannot process other documents.

Here we introduce several common operations in the aggregation framework:

    • $project: Modifies the structure of the input document. You can use it to rename, add, or delete fields, or to create calculations and nested documents.
    • $match: Used to filter data and only output documents that match the criteria. $match a standard query operation using MONGODB.
    • $limit: Used to limit the number of documents returned by the MongoDB aggregation pipeline.
    • $skip: Skips the specified number of documents in the aggregation pipeline and returns the remaining documents.
    • $unwind: Splits one of the array type fields in the document into multiple bars, each containing a value in the array.
    • $group: Groups The documents in the collection to be used for statistical results.
    • $sort: Sorts the input documents after the output.
    • $geoNear: Outputs an ordered document that is close to a geographic location.

Pipe operator Instance

1. $project Example

Db.article.aggregate (

{$project: {

Title:1,

Author:1,

}}

);

In this case, there are only _id,tilte and author three fields in the result, and by default _id fields are included, if you want to not include _id, you can:

Db.article.aggregate (

{$project: {

_id:0,

Title:1,

Author:1

}});

2. $match instance

Db.articles.aggregate ([

{$match: {score: {$gt: $, $lte: 90}},

{$group: {_id:null, Count: {$sum: 1}}}

] );

$match is used to get a score greater than 70 less than or equal to 90 records, and then the qualifying record is sent to the next stage of the $group pipeline operator for processing.

3. $skip instance

Db.article.aggregate (

{$skip: 5});

After the $skip pipeline operator is processed, the first five documents are "filtered" out.

MongoDB Aggregations and pipelines

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.