MongoDB Learning Summary (III.)--Common aggregation functions

Source: Internet
Author: User
Tags emit

The previous article introduced the MongoDB additions and deletions to change the basic usage of the command, this article to learn some of MongoDB's basic aggregation functions.

Here we go straight to the topic, with a simple example to introduce in turn.

1.count () function

The Count function of the collection is the simplest aggregate function that returns the number of documents in the collection.

2.distinct () function

Used to find all the different values of a given key in a collection.

We can also execute the DISTINCT aggregate function by running commands on the database, at which point we must specify the collection and key:

In the command parameter, the key "distinct" specifies the collection name of the statistic, and the key "key" specifies the name of the statistic key! Returns a document with the key "value" specifying all the values in the collection for the key of the statistic! We can also see that the index is also used in the statistics.

3.group () function

Group aggregation enables a slightly more complex operation by grouping the documents in the collection by the specified key, and then generating the final result document by aggregating all the documents in each group.

The following is the group for age in the student collection and then processes each set of documents to find the corresponding name.

Key: Specifies the name of the key to be used for grouping.

Initial: Sets the initialization value for each group after grouping.

$reduce: function, the first parameter is the current document object, and the second parameter is the summation of the document Loop. Each grouping is aggregated according to the required conditions, and each group has a separate accumulator document that records the aggregated results.

We can also do this by running commands on the database

"NS": Specifies the collection name.

The above is a grouping of all records in a collection, and of course it can be grouped according to certain conditions. Below we group name is jack,lucy,james .

Here we see filtering by Condition conditions, only grouping documents whose names are jack,lucy,james ,condition can use abbreviated cond or Q here .

In addition, we see that there is also a parameter finalize, which is intended to be processed again after the $reduce aggregation, resulting in the desired result.

4.MapReduce function

MapReduce is the most complex and flexible of the aggregations, andcount,distince,group can do it.

Map: Map functions, map operations to each document in the collection, and the collection will be grouped according to the key you specify. Using function emit (Key,value)

Reduce: The simplification function will group the data after the map is grouped, until the list of each key has only one value, and the result set is returned.

MapReduce: The last execution function, the parameter is map,reduce.

The concept is not easy to understand, see the example is very clear.

As you can see from this example,map uses the function emit (key,value) to group the mappings according to the name Key, and each key corresponds to The key value of the array {count:1}.

The reduce function then operates on the corresponding key value for each key. Finally get the desired result set.

the last MapReduce function outputs this result set.

Let's look at the value of this result set.

Commonly used aggregation functions are introduced here, the next one to learn some basic usage of the index.

MongoDB Learning Summary (III.)--Common aggregation functions

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.