A brief analysis of group group _mongodb in MongoDB

Source: Internet
Author: User
Tags emit mongodb prev

The aggregation made by group is somewhat complex. Select the key on which the group is based, and then mongodb the collection according to the selected key values to several groups. You can then produce a result document by aggregating the documents within each group.
Like databases, group is often used for statistics. MongoDB's group has many limitations, such as: The return result set cannot exceed 16M, the group operation will not process more than 10,000 unique keys, as if the index is not yet available [not very sure].

Group needs about a few parameters.

1.key: A field used to group documents. And Keyf both must have a
2.KEYF: You can accept a JavaScript function. A field that is used to dynamically determine a grouped document. And key both must have a
Using the initialization of variables in 3.initial:reduce
4.reduce: The reduce function is executed. function needs to return a value.
5.cond: Filter conditions are performed.
6.finallize: When reduce execution completes, the result set returns the function that was last executed on the result set. Optional.
Here's an example:
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. General Group Query

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

3, the ordinary $where inquiry:

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 with function return values

Note that $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++}}}
);

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

First insert test data for
(Var i=1;i<21;i++)
{
Db.test.insert ({_id:i,name: ' mm ' +i});
}
Perform MapReduce
Db.runcommand (
{
mapreduce: ' Test ',
map:function () {Emit (THIS.NAME.SUBSTR) , this);},
reduce:function (key,vals) {return vals[0];},//Note: Vals is an object rather than 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 handled 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 you execute the mapreduce command, 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"}}

The above mentioned is the entire content of this article, I hope you can enjoy.

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.