As with databases, group is often used for statistics. MongoDB Group also has many restrictions, such as: The return result set can not exceed 16M, the group operation will not handle more than 10,000 unique keys, as if the index is not available [not very sure].
The group takes approximately a few parameters.
1.key: The field used to group the document. And Keyf both must have a
2.KEYF: You can accept a JavaScript function. The field used to dynamically determine the grouped document. And key both must have a
Initialization of variables used in 3.initial:reduce
4.reduce: The reduce function is executed. The function needs to return a value.
5.cond: The condition to perform the filter.
6.finallize: At reduce execution completion, the result set returns the function that was last executed on the result set. Optional.
An example is described below:
Insert test data first:
1. General Group Query
Db.test.group ({ key:{age:true}, initial:{num:0}, $reduce: function (doc,prev) { prev.num++ }
2. Filter and then group
Db.test.group ({key:{age:true},initial:{num:0}, $reduce: function (doc,prev) {prev.num++},condition:{age:{$gt: 2}}) ; Db.runcommand ({group:{ns: "Test", key:{age:true},initial:{num:0}, $reduce: function (Doc,prev) {prev.num++}, condition:{age:{$GT: 2}}});
3, the ordinary $where query:
Db.test.find ({$where: function () {return this.age>2;}});
Group Federated $where Query
Db.test.group ({key:{age:true},initial:{num:0}, $reduce: function (doc,prev) {prev.num++},condition:{$where: function () {return this.age>2;}});
4. Grouping using function return values//Note that $KEYF the specified function must return an object
5. Using finalizers
Db.test.group ({$keyf: function (DOC) {return {age:doc.age};},initial:{num:0}, $reduce: function (doc,prev) {prev.num++ },finalize:function (DOC) {Doc.count=doc.num;delete doc.num;}}); Db.runcommand ({group:{ns: "Test", $keyf: function (DOC) {return {age:doc.age};},initial:{num:0}, $reduce: function (doc , prev) {prev.num++},finalize:function (doc) {Doc.count=doc.num;delete doc.num;}});
About MapReduce
Insert test Data First
for (Var i=1;i<21;i++) {Db.test.insert ({_id:i,name: ' mm ' +i});} Mapreducedb.runcommand ({mapreduce: ' Test ', map:function () {Emit (This.name.substr (0,3), this);},reduce:function (key,vals) {return vals[0];},//Note: Vals is an object instead of an array out: ' Wq '});
Note:
1.mapreduce is grouped according to the first parameter of the emit function called in the map function.
2. Key and document collections are processed by the reduce function only if a key matches multiple documents based on the grouping key. For example:
Db.runcommand ({mapreduce: ' Test ', map:function () {Emit (This.name.substr (0,3), this);},reduce:function (key,vals) { Return ' Wq ';},out: ' Wq '});
after executing the mapreduce command, review the WQ table data:
Db.wq.find () {"_id": "MM1", "Value": "Wq"} {"_id": "mm2", "Value": "Wq"} {"_id": "Mm3", "value": {"_id": 3, "Nam E ":" Mm3 "}} {" _id ":" MM4 "," value ": {" _id ": 4," name ":" Mm4 "}} {" _id ":" MM5 "," value ": {" _id ": 5," name ":" MM5 "}} {" _id ":" MM6 "," value ": {" _id ": 6," name ":" MM6 "}} {" _id ":" MM7 "," value ": {" _id ": 7," name ":" MM7 "} } {"_id": "MM8", "value": {"_id": 8, "name": "MM8"}} {"_id": "Mm9", "value": {"_id": 9, "name": "Mm9"}}