Insert test Data First: for (var i=1; i<20; i++) {var num=i%6; Db.test.insert ({_id:i,name: "User_" +i,age:num}); DescriptionIf you have _id this, MongoDB will not generate _ID, will use the _id you provide. 1. General packet Query Db.test.group ({key:{age:true},initial:{num:0}, $reduce: function (Doc,prev) {prev.num++}});
[ { " Age":1, "Num":4 }, { " Age":2, "Num":3 }, { " Age":3, "Num":3 }, { " Age":4, "Num":3 }, { " Age":5, "Num":3 }, { " Age":0, "Num":3 }]
Db.runcommand ({group:{ns: "Test", key:{age:true},initial:{num:0}, $reduce: function (Doc,prev) {prev.num++}}); 2. Filtering and then grouping 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}}}); normal $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;} }); 3. Use function return value Group//note, $KEYF the specified function must return an object Db.test.group ({$keyf: function (DOC) {return {Age:doc.age };},initial:{num:0}, $reduce: function (Doc,prev) {prev.num++}}), db.runcommand ({group:{ns: "Test", $keyf: Function (DOC) {return {age:doc.age};},initial:{num:0}, $reduce: function (Doc,prev) {prev.num++}}}); 4. Use Terminator 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});
}
For MapReduce
Db.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 '
});
Attention:
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, "name": "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"}} Reference: Http://www.cnblogs.com/mxw09/archive/2011/08/31/2161457.htm Lhttp://www.cnblogs.com/huangxincheng/archive/2012/02/21/2361205.html
MongoDB Group Group