MongoDB Aggregation Operations (group, aggregate, MapReduce operations)

Source: Internet
Author: User

#MongoDb Aggregation method group Aggrate mapreduce#1. Group (shard not supported, distributed calculation)*Grammatical Structure<pre>Db.collection.group ({key:{category:1},//Classification according to categorycond:{shop_price:{$gt: 20}},//a product with an additional condition of more than 20Reducefunction(Curr, result) {//Curr identifies a row of records, the result of a custom variable,}, Initial:{total:0},//initialize variables, variables defined here can be obtained in the reduce method, Result.totalFinalizefunction(Result) {//after the final grouping, a result is obtained and the result is processed        }    }); </pre> *working with Max<pre>Db.collection.group ({key:{category:1}, # Classification according to category cond:{}, # Additional conditional commodity borrow a commodity greater than 20 reduce:function(Curr, result) {# Curr identifies a row of records, a result-custom variable,if(Curr >result.total) $result. Total=Curr.shop_price; }, Initial:{total:0}, # Initialize variables, variables defined here can be obtained in the reduce method, Result.total Finalize:function(Result) {# After the last grouping, a result is obtained, then the result is processed}}); </pre> *Handling Avg<pre>Db.collection.group ({key:{category:1}, # classify cond:{shop_price:{$GT according to Category:20}}, # Additional conditional commodity borrow a commodity greater than 20 reduce:function(Curr, result) {# Curr identifies a row of records, a result-custom variable, Result.count+ = 1; Result.total+=parsefloat (Curr.shop_price); }, Initial:{total:0,count:0,avg:0}, # Initialize variables, variables defined here can be obtained in the reduce method, Result.total Finalize:function(Result) {# After the last grouping, a result is obtained and the result is processed result.avg= Result.total/Result.count;    }            }); </pre>2. Aggregate*Grammatical Structure<pre>db.collection.aggregate ([{}, {}, .......,]); </pre> *$group a select in SQL. According to the CAT_ID and color classification of the goods, and then find out the inventory quantity of the classified goods<pre>[{$group: {_id:{cat_id:"$cat _id", Color: "$color"}, total:{$sum: "$goods _number"}}}            ]        </pre> *$project like a select in SQL, take out the cat_id,goods_name of the commodity table and do not go out of the _id field<pre>[{$project: {_id:0, Cat_id:1, goods_name:1}}        ]        </pre> *$match match criteria to take out a product with a category ID of 3 or a price greater than 200 yuan<pre>[{$math: {$or: [{cat_id:3}, {price:{$GT: 200}}]}}        ]        </pre> *$unwind the Split field, (the field must be an array, otherwise an error)<pre>Data model:shoes {_id:1, feature:["man", "big", "Black", "cheap"]} Command:db.shoes.aggregate ([{$unwind:"$feature"}]) Output: {_id:1,feature: "Man"} {_id:1,feature: "Big"} {_id:1,feature: "Black"} {_id:1,feature: "Cheap"}        </pre> * $redact [Edit condition] (http://docs.mongodb.org/manual/reference/operator/aggregation/redact/#pipe. _s_redact)*only 30 records are $limit restricted conditions<pre>[{$limit:30}        ]        </pre> * $sort sorted according to commodity inventory positive order, 1/positive sequence -1/Reverse<pre>[{$sort: {goods_number:1,}} ]        </pre> * $geoNear [distance calculation] (http://docs.mongodb.org/manual/reference/operator/aggregation/geonear/#pipe. _s_geonear)*Skip 20 lines $skip Skip Rows<pre>[{$skip:20}        ]        </pre> *$out Output The matching results to another collection, take the item with a price greater than 3000 out of the Cat_id,goods_name,goods_number field, do not go out of the _id field, and then sort by the cat_id,color of the item, and the result skips a row. Take only 30 hang records and push the results into the result set<pre>[{$math: {goods_price:{$gte:3000}}}, {$project: {cat_id:1,goods_name:1, Goods_number:1, _id:0}}, {$group: {_id:{cat_id:"$cat _id", Color: "$color"}, total:{$sum: "$goods _number"}}}, {$skip:1}, {$limit:30}, {$out:"Result"}        ]        </pre>3. MapReduce Method*Grammatical Structure<pre>Db.collection.mapReduce (function() {Emit ( This. cat_id, This. Goods_number); }, //Map Method                function(key,value) {returnarray.sum (value); }, //Reduce Method{query:{goods_id:{$gt:30}}, out:"Mapreduceresultset"                }            ); </pre> * [More Information] (http://docs.mongodb.org/manual/core/map-reduce/)

MongoDB Aggregation Operations (group, aggregate, MapReduce 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.