发布网友 发布时间:2022-05-07 02:05
共2个回答
懂视网 时间:2022-05-07 06:26
mongodb聚合函数 1.count 这个函数很好理解,和其他数据库一样,就是做一个count操作 select count(*) from tab1 =db.tab1.count() 以上例子很好理解,但是如果在操作中有skip,limit这种操作的话,那么count会忽略掉这些操作,必须加入一个true参数 比如:d
mongodb聚合函数
1.count
这个函数很好理解,和其他数据库一样,就是做一个count操作
select count(*) from tab1 =db.tab1.count()
以上例子很好理解,但是如果在操作中有skip,limit这种操作的话,那么count会忽略掉这些操作,必须加入一个true参数
比如:db.tab1.find().skip(1),limit(2).count()和db.tab1.find().count()得出的结果是一样的
必须修改为db.tab1.find().skip(1).limit(2).count(true)
2.distinct
去重函数,这个也应该很熟悉,各种关系数据库都有
具体操作:db.runCommand({"distinct":"collection","key":"xxxx"})=db.collections.distinct("xxxx");
3.group
select a,b,sum(c) csum from coll where active=1 group by a,b
db.coll.group( {key: { a:true, b:true },
cond: { active:1 },
reduce:function(obj,prev){ prev.csum += obj.c; },
initial: { csum: 0 }
});
参数解释:
Key:group字段
Reduce:聚合函数,典型的有sum和count操作,包含两个参数,当前遍历对象以及聚合计数器
Initial:初始化计数器值
Cond:查询条件筛选选项,如果是空,那么默认对当前collection的所有行操作
Group不能在sharding中使用,结果输出不能多于10000个keys
举例:
> db.mtb1.find()
{ "_id" : ObjectId("4fb4be52aa814943b788095c"), "name" : "aaa", "score" : 77 }
{ "_id" : ObjectId("4fb4be5caa814943b788095d"), "name" : "bbb", "score" : 79 }
{ "_id" : ObjectId("4fb4be61aa814943b788095e"), "name" : "ccc", "score" : 79 }
{ "_id" : ObjectId("4fb4be69aa814943b788095f"), "name" : "ddd", "score" : 79 }
{ "_id" : ObjectId("4fb4be74aa814943b7880960"), "name" : "eee", "score" : 100 }
{ "_id" : ObjectId("4fb4be7aaa814943b7880961"), "name" : "fff", "score" : 100 }
{ "_id" : ObjectId("4fb4be82aa814943b7880962"), "name" : "ggg", "score" : 100 }
{ "_id" : ObjectId("4fb4be9eaa814943b7880963"), "name" : "hhh", "score" : 200}
> db.mtb1.group(
... {key:{score:true},
... reduce:function(obj,prev){prev.count++;},
... initial:{count:0}
... });
[
{
"score" : 77,
"count" : 1
},
{
"score" : 79,
"count" : 3
},
{
"score" : 100,
"count" : 3
},
{
"score" : 200,
"count" : 1
}
]
一个简单的例子,应该很明白了。其实这里group还是建立在reduce上面
4.map/reduce
这个工具的功能可以实现以上三种任何一种,它是分两步完成,先做map,再reduce,map首先对数据进行分解映射,map执行完,紧接着会进行reduce,map后会的到key-values,像{key:[value1,value2,value3...]},然后通过reduce处理后得到key-value,也就是单一的值。
语法:
db.runCommand(
{ mapreduce :
map :
reduce :
[, query :
[, sort :
[, limit :
[, out :
[, keeptemp:
[, finalize :
[, scope :
[, verbose : true]
}
);
query是很常用的,它用来在map阶段过滤查询条件的以限定MapReduce操作的记录范围,sort和limit集合query使用。
out指定输出结果的collections名称
Keeptemp 布尔型,默认是false,如果是true那么生成的collection是永久存在的,如果是false,那么在客户端连接关闭后,会自动删除生成的collection
Finalize一般用来计算平均数,裁剪数组,清除多余信息
query是很常用的,它用来在map阶段过滤查询条件的以限定MapReduce操作的记录范围,sort和limit集合query使用。
out指定输出结果的collections名称
Keeptemp 布尔型,默认是false,如果是true那么生成的collection是永久存在的,如果是false,那么在客户端连接关闭后,会自动删除生成的collection
Finalize一般用来计算平均数,裁剪数组,清除多余信息
{ result :
counts : {
input :
emit :
output :
} ,
timeMillis :
ok : <1_if_ok>,
[, err :
}
result:储存结果的collection的名字
input:满足条件的数据行数
emit:emit调用次数,也就是所有集合中的数据总量
ouput:返回结果条数
timeMillis:执行时间,毫秒为单位
ok:是否成功,成功为1
err:如果失败,这里可以有失败原因
给出官方文档的一个例子:
$ ./mongo
> db.things.insert( { _id : 1, tags : ['dog', 'cat'] } );
> db.things.insert( { _id : 2, tags : ['cat'] } );
> db.things.insert( { _id : 3, tags : ['mouse', 'cat', 'dog'] } );
> db.things.insert( { _id : 4, tags : [] } );
> // map function
> m = function(){
... this.tags.forEach(
... function(z){
... emit( z , { count : 1 } );
... }
... );
...};
> // reduce function
> r = function( key , values ){
... var total = 0;
... for ( var i=0; i ... total += values[i].count; ... return { count : total }; ...}; > res = db.things.mapReduce(m,r); > res {"timeMillis.emit" : 9 , "result" : "mr.things.1254430454.3" , "numObjects" : 4 , "timeMillis" : 9 , "errmsg" : "" , "ok" : 0} > db[res.result].find() {"_id" : "cat" , "value" : {"count" : 3}} {"_id" : "dog" , "value" : {"count" : 2}} {"_id" : "mouse" , "value" : {"count" : 1}} > db[res.result].drop()
热心网友
时间:2022-05-07 03:34
// ※※※※※※※※※※※※※※※※※※※ 数据加载 ※※※※※※※※※※※※※※※※※※※
db.proc.remove();
for(var i = 10; i < 1020; i++){db.proc.insert({class:"01", val:i, name: "name" + i})};
for(var i = 10; i < 1030; i++){db.proc.insert({class:"02", val:i, name: "name" + i})};
for(var i = 10; i < 1040; i++){db.proc.insert({class:"03", val:i, name: "name" + i})};
for(var i = 10; i < 1050; i++){db.proc.insert({class:"04", val:i, name: "name" + i})};
for(var i = 10; i < 1060; i++){db.proc.insert({class:"05", val:i, name: "name" + i})};
for(var i = 10; i < 1070; i++){db.proc.insert({class:"06", val:i, name: "name" + i})};
for(var i = 10; i < 1080; i++){db.proc.insert({class:"07", val:i, name: "name" + i})};
for(var i = 10; i < 1090; i++){db.proc.insert({class:"08", val:i, name: "name" + i})};
for(var i = 10; i < 1100; i++){db.proc.insert({class:"09", val:i, name: "name" + i})};
for(var i = 10; i < 1110; i++){db.proc.insert({class:"10", val:i, name: "name" + i})};
for(var i = 10; i < 1120; i++){db.proc.insert({class:"11", val:i, name: "name" + i})};
// ※※※※※※※※※※※※※※※※※※※ mapRece ※※※※※※※※※※※※※※※※※※※
m = function(){emit(this.class, {count:1, max:this.val, sum:this.val})}
r = function(key, values){
var ct = 0, sm = 0, mx = 0;
for(var i = 0; i < values.length; i++){
ct += values[i].count;
sm += values[i].max;
mx = Math.max(mx, values[i].max);
}
return {count:ct, max: mx, sum:sm};
}
// ※※※※※※※※※※※※※※※※※※※ 数据处理 ※※※※※※※※※※※※※※※※※※※
res = db.runCommand({maprece:"proc", map:m, rece:r, out:"proc_res"})
res = db.runCommand({maprece:"proc", map:m, rece:r, out:"proc_res3", query:{"class":{$gt:"03"}}})
res = db.runCommand({maprece:"proc", map:m, rece:r, out:"proc_res5", query:{"class":{$gt:"05"}}})
res = db.runCommand({maprece:"proc", map:m, rece:r, out:"proc_res6", query:{"class":{$gt:"06"}}})
res = db.runCommand({maprece:"proc", map:m, rece:r, out:"proc_res9", query:{"class":{$gt:"09"}}})
res = db.runCommand({maprece:"proc", map:m, rece:r, out:"proc_res10",query:{"class":{$gt:"10"}}})
// ※※※※※※※※※※※※※※※※※※※ 结果查看 ※※※※※※※※※※※※※※※※※※※
db.proc_res.find()
{ "_id" : 1, "value" : { "class" : 1, "count" : 10, "sum" : 145, "max" : 19 } }
{ "_id" : 2, "value" : { "class" : 2, "count" : 20, "sum" : 390, "max" : 29 } }
{ "_id" : 3, "value" : { "class" : 3, "count" : 30, "sum" : 735, "max" : 39 } }
{ "_id" : 4, "value" : { "class" : 4, "count" : 40, "sum" : 1180, "max" : 49 } }
{ "_id" : 5, "value" : { "class" : 5, "count" : 50, "sum" : 1725, "max" : 59 } }
{ "_id" : 6, "value" : { "class" : 6, "count" : 60, "sum" : NaN, "max" : NaN } }
{ "_id" : 7, "value" : { "class" : 7, "count" : 70, "sum" : 3115, "max" : 79 } }
{ "_id" : 8, "value" : { "class" : 8, "count" : 80, "sum" : NaN, "max" : NaN } }
{ "_id" : 9, "value" : { "class" : 9, "count" : 90, "sum" : NaN, "max" : NaN } }
{ "_id" : 10, "value" : { "class" : 10, "count" : 100, "sum" : NaN, "max" : NaN } }
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
db.proc_res3.find()
{ "_id" : 4, "value" : { "class" : 4, "count" : 40, "sum" : 1180, "max" : 49 } }
{ "_id" : 5, "value" : { "class" : 5, "count" : 50, "sum" : 1725, "max" : 59 } }
{ "_id" : 6, "value" : { "class" : 6, "count" : 60, "sum" : NaN, "max" : NaN } }
{ "_id" : 7, "value" : { "class" : 7, "count" : 70, "sum" : NaN, "max" : NaN } }
{ "_id" : 8, "value" : { "class" : 8, "count" : 80, "sum" : 3960, "max" : 89 } }
{ "_id" : 9, "value" : { "class" : 9, "count" : 90, "sum" : 4905, "max" : 99 } }
{ "_id" : 10, "value" : { "class" : 10, "count" : 100, "sum" : NaN, "max" : NaN } }
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
db.proc_res5.find()
{ "_id" : 6, "value" : { "class" : 6, "count" : 60, "sum" : 2370, "max" : 69 } }
{ "_id" : 7, "value" : { "class" : 7, "count" : 70, "sum" : NaN, "max" : NaN } }
{ "_id" : 8, "value" : { "class" : 8, "count" : 80, "sum" : NaN, "max" : NaN } }
{ "_id" : 9, "value" : { "class" : 9, "count" : 90, "sum" : 4905, "max" : 99 } }
{ "_id" : 10, "value" : { "class" : 10, "count" : 100, "sum" : 5950, "max" : 109 } }
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
db.proc_res6.find()
{ "_id" : 7, "value" : { "class" : 7, "count" : 70, "sum" : 3115, "max" : 79 } }
{ "_id" : 8, "value" : { "class" : 8, "count" : 80, "sum" : NaN, "max" : NaN } }
{ "_id" : 9, "value" : { "class" : 9, "count" : 90, "sum" : NaN, "max" : NaN } }
{ "_id" : 10, "value" : { "class" : 10, "count" : 100, "sum" : NaN, "max" : NaN } }
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
db.proc_res9.find()
{ "_id" : 10, "value" : { "class" : 10, "count" : 100, "sum" : 5950, "max" : 109 } }
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }
db.proc_res10.find()
{ "_id" : 11, "value" : { "class" : 11, "count" : 110, "sum" : NaN, "max" : NaN } }