日期分组 db.msds_acce***ecord.group({ keyf : function(doc){ var date =
日期分组
db.msds_acce***ecord.group({
keyf : function(doc){ var date = new Date(doc.addtime); var dateKey = ""+date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate(); return {'day':dateKey}; //33},
initial : {"count":0},
reduce : function Reduce(doc, out) { if(doc.url){ out.count +=1; }
}
});
先插入测试数据:
for(var i=1; i<20; i++){
var num=i%6;
db.test.insert({_id:i,name:"user_"+i,age:num});
}
1.普通分组查询
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.筛选后再分组
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}}
}
});
普通的$where查询:
db.test.find({$where:function(){
return this.age>2;
}
});
group联合$where查询
db.test.group({key:{age:true},initial:{num:0},$reduce:function(doc,prev){
prev.num++
},
condition:{$where:function(){
return this.age>2;
}
}
});
3.使用函数返回值分组
//注意,$keyf指定的函数一定要返回一个对象
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.使用终结器
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
//首先插入测试数据
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];}, //注意:vals是一个Object对象而不是数组
out:'wq'
});
注意:
1.mapreduce是根据map函数里调用的emit函数的第一个参数来进行分组的
2.仅当根据分组键分组后一个键匹配多个文档,才会将key和文档集合交由reduce函数处理。例如:
db.runCommand(
{
mapreduce:'test',
map:function(){emit(this.name.substr(0,3),this);},
reduce:function(key,vals){return 'wq';},
out:'wq'
});
执行mapreduce命令后,再查看wq表数据:
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" } }
--结束END--
本文标题: mongodb 常用命令 日期, 分组
本文链接: https://lsjlt.com/news/43414.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0