Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sample Analysis of aggregation in mongodb

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the example analysis of aggregation in mongodb, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

One: aggregation

Common aggregation operations are the same as sql server: count,distinct,group,mapReduce.

Count

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

Distinct

I believe everyone is very familiar with this operation, who specified, who can not be repeated, directly above the picture.

Group

It's a little complicated to do group operation in mongodb, but if you are familiar with group in sql server, you can still catch a glimpse.

As you can see, in fact, group operation essentially forms a "KMurv" model, just like Dictionary in C#. Well, with this kind of thinking.

Let's look at how to use group.

The following example is to group according to age, where value is the name of the corresponding age. Here is an introduction to these parameters:

Key: this is the key of grouping. We are grouping age groups here.

Initial: each group shares an "initialization function". Note that each group, such as age=20 's value's list, shares one.

The initial function, age=22 also shares an initial function.

Reduce: the first argument to this function is the current document object, and the second argument is the cumulative object from the last function operation, the first time

Is {"perosn": []} in initial. $reduce will be called as many times as there are documents.

Seeing the above results, does it feel a little bit that we have found the corresponding name personnel through age, but sometimes we may have the following requirements:

①: want to filter out some people with age > 25.

②: sometimes there are too many people in the person array. I'd like to add a count attribute to indicate it.

In response to the above requirements, it is easy to do in group, because group has two optional parameters: condition and finalize.

Condition: this is the filter condition.

Finalize: this is a function that often triggers this method after each set of documents is executed, so it is its job to add count to each set of collections.

MapReduce

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

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

① map:

This is called the mapping function, which will call emit (key,value), and the collection will map and group according to the key you specify.

② reduce:

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

The key,vlaue in emit is the collection of emit (value) grouped by emit, which is an array of many {"count": 1}.

③ mapReduce:

This is the last function executed, with arguments to map,reduce and some optional parameters. If you look at the picture, you can see:

From the figure, we can see the following information:

Result: "stored collection 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: finally returns the number of documents.

Finally, let's look at the grouping by name in the "collecton" collection.

Two: Vernier

The cursors in mongodb are somewhat similar to what we call delayed execution in C#, such as:

Var list=db.person.find ()

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

For or next () is loaded at once, and then the cursor is read line by line. When we have finished enumerating, the cursor is destroyed, and then when we get it through list

It was found that no data was returned.

Of course, our "query construction" can also be a little more complicated, such as paging and sorting can be added.

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

Then such a "query construction" can be executed when we need to execute, greatly increasing the unnecessary cost.

Thank you for reading this article carefully. I hope the article "sample Analysis aggregated in mongodb" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report