MongoDB Group Aggregation Query

Source: Internet
Author: User

Use aggregations, db. Collection name. Aggregate ... Rather than find.

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.

Each operator (the collection) takes a series of documents, makes some type conversions on those documents, and finally passes the converted document as a result to the next operator, and for the last operator, returns the result to the client

Grouping (the field name of the Group field $+ is established here)
As you can see here, Lastmodifybyusercode as a unique identifier _id, the field count calculates the number of occurrences of the same lastmodifybyusercode with $sum

Db.test.aggregate ({' $group ': {' _id ': ' $lastModifyByUserCode ', ' count ': {' $sum ': 1}}})

Grouping by multiple fields

Db.test.aggregate ({$group: {_id:{lastmodifybyusercode: ' $lastModifyByUserCode ', createtime: ' $createTime '}}})

Show additional fields (must use aggregation [$sum, $avg, $first, $last, $max, $min, $push, $addToSet, $stdDevPop, $stdDevSamp])

Db.test.aggregate ({' $group ': {' _id ': ' $lastModifyByUserCode ', ' count ': {' $sum ': 1}, ' Createtime ': {' $first ': ' $ Createtime "}}})

Cast, you can rename the field to create a copy of the field (note: When renaming a field, MongoDB does not record the history name of the field, so if there is an index on the "Orginalfieldname" field, The aggregation framework cannot use this index in the following sort operation, so you should try to use the index before modifying the field name .

Db.test.aggregate ({$project: {createTime2: ' $createTime ', CreateTime3: ' $createTime ', _id:0}})

$match is used to filter the collection of documents, supports find-like filters, cannot use geospatial operators in $match, (as far as possible to put $match in the first of multiple aggregations, so that the index can be effectively used)

And according to official documentation, if full-text indexing is used in $match, then the aggregations that follow are no longer used $text

MongoDB Group Aggregation Query

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.