8 days to learn MongoDB-advanced operations on the third day

Source: Internet
Author: User

Today, I will share with you some interesting things in mongodb, including aggregation and cursors.

1. Aggregation

Common aggregate operations are the same as SQL server, including count, distinct, group, and mapReduce.

<1> count

Count is the simplest, easiest, and most commonly used aggregate tool. Its usage is exactly the same as that in C.

 

<2> distinct

This operation is also very familiar to everyone. If it is specified, no one can repeat and directly.

 

<3> group

The group operation in mongodb is a little complicated. However, if you are familiar with the group operation in SQL server, you should take a look.

Clearly, in fact, group operations are essentially a "k-v" model, just like the Dictionary in C,

Let's take a look at how to use group.

In the following example, the group operation is performed according to age, and the value is the name of the corresponding age. The following describes these parameters:

Key: this is the group key. Here we are grouping ages.

Initial: each group shares an "initialization function". Note: Each group shares an "initialization function", for example, a list of values of age = 20.

Initial function. age = 22 shares an initial function.

$ Reduce: the first parameter of this function is the current document object, and the second parameter is the cumulative object of the last function operation. The first time

Is {"perosn": []} in initial. How many documents will be called by $ reduce.

If we see the above results, we can see the corresponding name personnel through age, but sometimes we may have the following requirements:

①: To filter out age> 25 employees.

②: Sometimes there are too many people in the person array. I want to add a count attribute to indicate it.

To meet the above requirements, we can still do well in the group, because the group has two optional parameters: condition and finalize.

Condition: This is the filtering condition.

Finalize: This is a function. After each set of documents is executed, this method is triggered. Therefore, adding count to each set is the function.

 

<4> mapReduce

This is the most complex aggregate function, but the more complicated it is, the more flexible it is.

MapReduce is actually a programming model used in distributed computing. It has a "map" function and a "reduce" function.

① Map:

This is called a ing function, which calls emit (key, value). The set will be mapped to the group according to the specified key.

② Reduce:

This is called a simplification function, which simplifies grouping of data after map grouping. Note: The key in reduce (key, value) is

In emit, the key and vlaue are the set of emit (value) after the emit group. Here there are many {"count": 1} arrays.

③ MapReduce:

This is the final execution function. The parameters are map, reduce, and some optional parameters. The specific figure shows:

 

We can see the following information:

Result: "Name of the saved set";

Input: Number of input documents.

Emit: The number of times this function is called.

Reduce: The number of times this function is called.

Output: the number of returned documents.

Finally, let's take a look at the group by name in the "collecton" set.

 

Ii. cursor

The cursor in mongodb is a bit similar to the C # In which execution is delayed, for example:

Var list = db. person. find ();

For such an operation, the list does not actually get the document in person, but declares a "query structure ".

For or next () is loaded at a time, and then the cursor is read row by row. After the enumeration is complete, the cursor is destroyed. When we get the cursor through list,

No data is returned.

 

Of course, our "query structure" can also be used for complex points, such as paging and sorting.

Var single = db. person. find (). sort ({"name", 1}). skip (2). limit (2 );

This "query structure" can be executed when we need to execute it, greatly increasing unnecessary costs.

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.