MongoDB: a detailed explanation of mongodb's advanced operations, aggregation, and cursors

Source: Internet
Author: User

A few days ago, I summarized the mongodb installation entry and explained in detail the basic operations for adding, deleting, modifying, and querying. Today I will summarize more advanced operations, aggregation, and cursors for mongodb.

I. Aggregation: mongodb's aggregation operations are generally divided into four scenarios: count, distinct, group, and mapReduce

1. count

Count is the simplest, easiest, and most commonly used aggregate tool. It is usually used by everyone, so it is not a problem for everyone, so it is the simplest ~~

countdb.person.find()db.person.count()db.person.count({"age":40})

2. distinct <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + ucvD + fingerprint + hozwvcD4KPHA + PHByZSBjbGFzcz0 = "brush: SQL;"> db. person. distinct ("age ")

3. group

Group operations are a little complicated, but the more complicated the operation, the more flexible the application is. group operations are essentially equivalent to a "key-value" model.

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 initial function, for example, the list of values of age = 10, age = 40 also shares an 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 parameter is {"perosn": []} in initial. How many documents will be called by $ reduce.

groupdb.person.group({"key":{"age":true},"initial":{"person":[]},"$reduce":function(cur,prev){  prev.person.push(cur.name);}})



The above results may sometimes have other requirements, such:

1. filter out age <26 years old members.

2. Sometimes people have too many Members and I cannot view them one by one. So I want to count them. This looks more convenient.

This is very easy to do in group, because mongodb has two optional parameters: condition and finalize.

Condition is a filtering condition.

Finalize is a method that is triggered after each article is executed. You can add count to this method.

Condition and finalizedb. person. group ({"key": {"age": true}, "initial": {"person": []}, "$ reduce": function (cur, prev) {prev. person. push (cur. name) ;}, "finalize": function (count) {out. count = out. person. length ;}, "condition": {"age": {$ gte: 10 }}})


4. mapReduce

MapReduce is actually a programming model used in distributed computing. It has a "map" function and a "reduce" function.
1. 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.
2. 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.
3. mapReduce:
This is the final execution function. The parameters are map, reduce, and some optional parameters. The specific figure shows:

mapReducemapfunction(){emit(this.name,{count:1});}reducefunction(key,value){var result={count:0};for(var i=0;i<value.length;i++){result.count += value[i].count;}return result;}db.person.mapReduce(map,reduce,{"out":"collection"}){"result":"collection","timeMillis":15,"counts":{"input":7,"emit":7,"reduce":3,"output":4},"ok":1,}
Seo8L3A + CjxwPiBtb25nb2RiwO/release + MrHyerD99K7uPahsLLp0a + release/release + release "brush: SQL;"> cursor var list = db. person. find (); list. forEach (function (x) {print (x. name );})

At the same time, we can also query by PAGE and sort!

This reduces unnecessary overhead.

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


Well, aggregation and cursors are basically the same. If there is something wrong, please correct it.

Original article, reproduced please indicate the source: http://blog.csdn.net/jessonlv

In the next phase, I will write operations such as mongodb index creation.

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.