MongoDB Learning Summary-3 (elaborate advanced operations)

Source: Internet
Author: User
Tags emit

Today, I would like to share with you the more interesting knowledge of MongoDB, including: aggregation, cursor.

One: Aggregation

Common aggregation operations are the same as SQL Server: Count,distinct,group,mapreduce.

<1> Count

Count is the simplest, easiest, and most commonly used aggregation tool, and its use is exactly the same as the count used in our C #.

<2> DISTINCT

This operation is believed that everyone is also very familiar with, designated who, who can not repeat, direct.

<3> Group

It's a little complicated to do group operations in MongoDB, but it's a bit more familiar to the group in SQL Server.

Can see clearly, in fact, group operation essentially formed a "k-v" model, like C # in the dictionary, good, with this thinking,

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

The following example is the group operation according to age, value is the name of the corresponding age. Here's a look at these parameters:

Key: This is the group key, we are here for the age group.

Initial: Each group shares an "initialization function", paying special attention to: Each group, such as the age=20 value of the list share a

Initial function, age=22 also shares a initial function.

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

is {"Perosn" in initial: []}. How many documents are there, and how many times $reduce will be called.

See the above results, is not a bit of feeling, we see through the age of the corresponding name of the person, but sometimes we may have the following requirements:

①: Want to filter out age>25 some people.

②: Sometimes there are too many people in the person array, and I want to add a Count property to indicate it.

For the above requirements, in the group is still very good, because the group has so two optional parameters: Condition and finalize.

Condition: This is the filter condition.

Finalize: This is a function, each set of documents after the execution, many will trigger this method, then in each set of collections plus count is its life.

<4> MapReduce

This is the most complex of the aggregation functions, but complex, the more complex the more flexible.

MapReduce is actually a programming model, used in distributed computing, where there is a "map" function, a "reduce" function.

①map:

This is called the mapping function, which calls emit (Key,value), and the collection is grouped according to the key you specify.

②reduce:

This is called a simplification function, which simplifies grouping the data after the map is grouped, and note that the key in reduce (Key,value) is

The Key,vlaue in emit is a collection of emit (value) after the emit is grouped, and here is an array of {"Count": 1}.

③mapreduce:

This is the last function to execute, with the parameters map,reduce and some optional parameters. See figure:

We can see the following information:

Result: "Stored set name";

Input: The number of incoming documents.

Emit: The number of times this function was called.

Reduce: The number of times this function was called.

Output: Returns the number of documents at the end.

Finally, let's take a look at the "Collecton" collection, grouped by name.

Two: Cursors

The cursor inside the MongoDB is somewhat similar to what we call a deferred execution in C #, such as:

var list=db.person.find ();

For such an operation, the list does not actually get the document in person, but instead declares a "query structure", which is passed when we need it.

For or next () loads it once, and then lets the cursor read through the line, and when we're done enumerating, the cursor is destroyed, and then when we get through the list,

found no data returned.

Of course, our "query construction" can also make complex points, such as paging, sorting can add in.

var single=db.person.find (). Sort ({"Name", 1}). Skip (2). Limit (2);

So this "query construct" can be executed when we need to execute it, which greatly increases unnecessary expenses.

MongoDB Learning Summary-3 (elaborate advanced operations)

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.