A brief analysis of the grouping of group in mongodb

  • 2020-05-24 06:24:57
  • OfStack

The aggregation that group does is a little bit complicated. After selecting the key on which to group, MongoDB divides the collection into groups based on the selected key values. You can then aggregate the documents in each group to produce one result document.
And database 1 like group is often used for statistics. MongoDB's group has many limitations, such as returning no more than 16M result sets, group operations that do not handle more than 10,000 unique 1 keys, and indexes that do not seem to be available [uncertain].

Group takes about 1.

1.key: fields used to group documents. And keyf have to have one of them
2.keyf: one javascript function is acceptable. Used to dynamically determine the fields to group documents. You have to have one of both, key and key
3.initial: initialization of variables is used in reduce
4.reduce: the reduce function that is executed. The function needs to return a value.
5.cond: conditions for performing filtering.
6.finallize: the function that is finally executed on the result set before reduce is returned. Optional.
Here is 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. Common grouping 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. Group after screening


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. General $where query:


db.test.find({$where:function(){
return this.age>2;
}
});

group joins $where queries


db.test.group({
key:{age:true},
initial:{num:0},
$reduce:function(doc,prev){
prev.num++
},
condition:{$where:function(){
return this.age>2;
}
}
});

4. Use function return values to group


// Pay attention to, $keyf Specified function 1 Need to return 1 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. Use 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; }
}
});

The relevant MapReduce


// First, insert the test data 
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 1 a Object Objects, not arrays 
out:'wq'
});

Note:

1.mapreduce is grouped according to the first argument of the emit function called in the map function
2. Only when one key matches multiple documents after grouping according to the grouping key, key and the document collection will be handled by the reduce function. Such as:


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

That's all for this article, I hope you enjoy it.


Related articles: