Use of the MapReduce function for MongoDB aggregation operations (11)

Source: Internet
Author: User
Tags emit

MapReduce is popular with the concept of "big data".

In fact, the concept of MapReduce is very simple,

Functionally, a group operation that is equivalent to an RDBMS

What are the real strengths of MapReduce?

A: Distributed, when the data is very large, like Google, there are n multi-data centers,

The data are not at one end of the Earth, with group grasp.

Since the group does not support distributed, the computing power of a single server is bound to be limited.

While Maprecuce supports distributed, supporting a large number of servers to work simultaneously,

Use brute force to count.

Maprecuce's working process:

①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 the simplification function, the data grouped by the map is grouped to simplify, note: The key in reduce (Key,value) is emit in the key,value for the emit grouping of emit (value), the array (the same group) of the data, the operation .

Calculate the total inventory for each column with MapReduce

Map function

var map = function () {

Emit (This.cat_id,this.goods_number);

}

var reduce = function (cat_id,numbers) {

return array.sum (numbers);

}

Db.goods.mapReduce (map,reduce,{out: ' Res '});

Results are saved to "res"

Db.res.find ()

#用mapReduce计算每个栏目下商品的平均价格

var map = function () {

Emit (This.cat_id,this.shop_price);

}

var reduce = function (cat_id,values) {

return Array.avg (values);

}

Db.goods.mapReduce (map,reduce,{out: ' Res '});

Use of the MapReduce function for MongoDB aggregation operations (11)

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.