Test condition: for Windows + MongoDB 1.8.2, insert the test data first: for (var I = 1; I <20; I ++) {var num = I % 6; db. test. insert ({_ id: I, name: "user _" + I, age: num});} 1. queries the database by common groups. test. group ({key: {age: true}, initial: {num: 0}, $ reduce: function (doc, prev) {prev. num ++}); db. runCommand ({group: {ns: "test", key: {age: true}, initial: {num: 0}, $ reduce: function (doc, prev) {prev. num ++ }}); 2. filter and then group the database. 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 }}}); Common $ where query: db. test. find ({$ where: function () {return this. age> 2 ;}}); group joins $ where to query db. test. group ({key: {age: true}, initial: {num: 0}, $ reduce: function (doc, prev) {prev. num ++}, condition :{$ where: function () {return this. age> 2 ;}}}); 3. grouping using function return values // note that the function specified by $ keyf 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 the 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 ;}}});
MapReduce
// Insert test data first
For (var I = 1; I <21; I ++)
{
Db. test. insert ({_ id: I, name: 'mm' + I });
}
// 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 rather than an array
Out: 'wq'
});
Note:
1. mapreduce groups emit functions based on the first parameter called by the map function.
2. Only when one key matches multiple documents according to the grouping key group, the key and document set are handed over to the reduce function for processing. For example:
Db. runCommand (
{
Mapreduce: 'test ',
Map: function () {emit (this. name. substr (0, 3), this );},
Reduce: function (key, vals) {return 'wq ';},
Out: 'wq'
});
Run the mapreduce command to view 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 "}}