About MongoDB group

Source: Internet
Author: User
Tags emit
Test data is inserted into the group of MongoDB: for (vari1; i20; I ++) {varnumi % 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. runCom

Test data is inserted into the group of MongoDB: for (var I = 1; i20; 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. runCom

About MongoDB group

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 });

}

1. query common groups

Db. 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

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 }}

}

});

Common $ where queries:

Db. test. find ({$ where: function (){

Return this. age> 2;

}

});

Group join $ 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. grouping with function return values

// Note that the $ keyf 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 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 "}}

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.