Mongodb Aggregation Basic Operations Example

Source: Internet
Author: User
Tags mongodb aggregate

MongoDB Two main operations: one is query, the other is statistics. For queries, it is primarily the find () method, which is combined with filters to combine multiple query criteria.

For statistics, it is mainly aggregate operations, such as group, SUM, AVG, project, match ...

Aggregate can be organized into pipeline form, followed by a variety of operations processing.

This article is a course note for MongoDB University M101, the main record: some common operations of MONGODB aggregate.

①project

It is a 1:1 operation, that is, a document input to project processing, the output of a new document. It primarily handles key processing (capitalization conversion, deletion of some key of the original document ...). )

For example, the original document is as follows:

{    "city": "Acmar",    "loc": [        -86.51557,        33.584132    ],    "pops": 6055,    "state": "AL",    " _id ":" 35004 "}

Want to turn it into:

{    "city": "Acmar",    "pops": 6055,    "state": "AL",    "Zip": "35004"}

Use the: Project operator for processing:

Db.zips.aggregate ([
City: {$toLower: "$city"}, Pop:1, state:1, zip: "$_id"}}
])
_id:0  Remove the _id field from the original document; city:{$ toLower: "$city"} The   value of "$city" in the original document is converted to lowercase and assigned to the new City field

Pop:1 State:1 indicates that the Pop field, state field in the original document is placed in the new document

Zip: "$_id" assigns the value of the ' _id ' field in the original document  to the new "zip" field  

②group avg, averaging by grouping. For example, a document format is as follows: Group the State field to average the population (pop) of each state

{    "city": "Fishers Island",    "loc": [            -72.017834,            41.263934    ],    "pops": 329,    "state": " NY ",    " _id ":" 06390 "}

Db.zips.aggregate ([
{"$group": {"_id": "$state", "Average_pop": {"$avg": "$pop"}}}
])

$group represents a grouping operation, and a new document is generated when the operation is performed.

_ID: $state represents grouping $state fields, the _id of the generated new document is the value of state
$avg: $pop indicates that the Pop field in the original document is averaged by $state grouping. The resulting average is the value of the "Average_pop" field.

The final results are as follows:

{"_id": "NY", "Average_pop": 9705.34} {"_id": "CT", "Average_pop": 13226.48} {"_id": "CA", "Average_pop": 19067.72} {"_id": "NJ", "Average_pop": 16949.9}

③match

The document example is as follows: you want to filter all records that have a population field (pop) greater than 100 000.

{    "city": "Acmar",    "loc": [        -86.51557,        33.584132    ],    "pops": 6055,    "state": "AL",    "_id": "35004"}

Db.zips.aggregate ([{$match: {    pop:{$gt: 100000}}    ])

$match means filtering the document

pop:{$gt: 100000} indicates filtering based on Pop field, filtered to a value greater than 100000 for pop

Mongodb Aggregation Basic Operations Example

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.