MongoDB 使用记录

一、建立索引(ensureIndex)

     在10w条这么简单的集合中查找一个文档要114毫秒有一点点让人不能接收,好,那么我们该如何优化呢?mongodb中给
db.test.ensureIndex({"username":1})
db.test.dropIndex({"username":1})

 

二、常用命令

 

MongoDB是不会回收空间的,但已删除的数据会标记删除,以后复用空间。
如果fileSize远大于dataSize,可以使用db.repairDatabase来删除一下多余的数据文件。不过这个指令执行时会对性能有较大影响

db.userdetails.remove({})


$project: 用于选择从收集的一些具体字段。
$match: 这是一个滤波操作,因此可以减少量,作为下一阶段的输入给定的文档。
$group: 如上所讨论的,这不实际的聚合。
$sort: 文件排序。
$skip: 与此有可能向前跳过的文件列表中的一个给定的的文档数量。
$limit: 这限制了的文档数量看一下由从当前位置开始的给定数
$unwind: 这是用来平仓文档的中使用数组。使用数组时,数据是一种pre-joinded,再次有个别文件,此操作将被取消。因此,这个阶段,数量会增加文件的下一阶段。

 

三、测试代码(未整理,先保留一下记录)

 

db.historydata.insert({"RecordTime":"2015-10-10 10:10:30","Records":[{"SpotID":"4","SpotValue":10},{"SpotID":"5","SpotValue":20},{"SpotID":"6","SpotValue":30}]})

db.historydata.aggregate({$match:{"RecordTime":{$gt:"2015-10-10 10:10:00"}}},
                 {$project:{c:0}},
                 {$unwind:"$Records"},
                 {$match:{"Records.SpotID":{$in:[1,2]}}},
                 {$group:{_id:"$_id",RecordTime:{"$first":"$RecordTime"},Records:{$push:"$Records"}}}
)
,"RecordTime":"$RecordTime"
db.historydata.aggregate({$project:{"Records":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':"1"}},{$group:{_id:"$_id","Records":{$push:"$Records"}}})

db.historydata.aggregate({$project:{"Records":1,"RecordTime":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':"1"}},{$group:{_id:"$_id","Records":{$push:"$Records"}}})

 

 

未为分组
db.historydata.aggregate({$project:{"Records":1,"RecordTime":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':{$in:["5","4"]}}})

 

 

分组
db.historydata.aggregate({$project:{"Records":1,"RecordTime":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':{$in:["1","5","4"]}}},{$group:{_id:"$RecordTime","Records":{$push:"$Records"}}})

 

 

for(var i=0;i<1000000;i++)
{
   var r='"Records":[';
   for(var j=0;j<10000;j++)
   {
       var v=i+j;
       r=r+'{"SpotID":"'+j+'","SpotValue":'+v+'},';
   }
   r=r+',{"SpotID":"10000","SpotValue":30}]';
   db.historydata.insert({RecordTime:i,eval(r));
}

for(var i=0;i<1000000;i++){
   var r='"Records":[';
   for(var j=0;j<10000;j++)
   {
       var v=i+j;
       r=r+'{"SpotID":'+j+',"SpotValue":'+v+'},';
   }
   r=r+'{"SpotID":10000,"SpotValue":30}]';
   r='{RecordTime:'+i+','+r+'}';
   db.historydata.insert(eval('('+r+')'));
}

for(var i=0;i<160000;i++){
   var r='"Records":[';
   for(var j=0;j<1000;j++)
   {
       var v=i+j;
       r=r+'{"SpotID":"'+j+'","SpotValue":"'+v+'"},';
   }
   r=r+'{"SpotID":"10000","SpotValue":"30"}]';
   var d=new Date(Date.parse("2015-10-10 10:10:00") + (1000 * i));
   var dd=d.getFullYear()+"-"+d.getMonth()+"-"+d.getDate()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
   r='{RecordTime:"'+dd +'",'+r+'}';
   db.historydata.insert(eval('('+r+')'));
}

db.historydata.ensureIndex({"RecordTime":1})

var ds=new Date();
for(var i=0;i<300000;i++){
   var r='"Records":[';
   for(var j=0;j<999;j++)
   {
       var v=i+j;
       r=r+'{"SpotID":"'+j+'","SpotValue":"'+v+'"},';
   }
   r=r+'{"SpotID":"999","SpotValue":"999"}]';
   var d=new Date(ds.getTime()+ (1000 * i * 60 * 10));
   if(d.getMonth()<10){
      mm="0"+d.getMonth();
   }else{
      mm=d.getMonth();
   }
   if(d.getDate()<10){
      dd="0"+d.getDate();
   }else{
      dd=d.getDate();
   }

   if(d.getHours()<10){
      hh="0"+d.getHours();
   }else{
      hh=d.getHours();
   }
   if(d.getMinutes()<10){
      mmm="0"+d.getMinutes();
   }else{
      mmm=d.getMinutes();
   }  
   if(d.getSeconds()<10){
      ss="0"+d.getSeconds();
   }else{
      ss=d.getSeconds();
   }

   var dd=d.getFullYear()+"-"+mm+"-"+dd+" "+hh+":"+mmm+":"+ss;
   r='{RecordTime:"'+dd +'",'+r+'}';
   db.historydata.insert(eval('('+r+')'));
}


new Date(Date.parse(dtTmp) + (1000 * Number))

var r='{name:"aa"}';db.historydata.insert(eval('('+r+')'));

db.historydata.aggregate({$match:{"RecordTime":{$gt:10000}},{$project:{"Records":1,"RecordTime":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':{$in:[1,5,4]}}},{$group:{_id:"$RecordTime","Records":{$push:"$Records"}}})

db.historydata.aggregate({$limit:5},{$project:{"Records":1,"RecordTime":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':{$in:[5,4]}}})

db.historydata.aggregate({$match:{"RecordTime":{$gt:10000}}},{$limit:5},{$project:{"Records":1,"RecordTime":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':{$in:[5,4]}}})

db.historydata.aggregate({$match:{"RecordTime":{$gt:10000,$lte:10005}}},{$limit:5},{$project:{"Records":1,"RecordTime":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':{$in:[5,4]}}})

db.historydata.aggregate({$match:{"RecordTime":{$gt:10000,$lte:10005}}},{$project:{"Records":1,"RecordTime":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':{$in:[5,4]}}})

 

 


db.historydata.aggregate({$match:{"RecordTime":{$gt:10000,$lte:10005}}},{$project:{"Records":1,"RecordTime":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':{$in:[5,4]}}},{$group:{_id:"$RecordTime","Records":{$push:"$Records"}}})

db.historydata.aggregate({$match:{"RecordTime":{$gt:10000,$lte:10005}}},{$project:{"Records":1,"RecordTime":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':{$in:[5,4]}}},{$group:{_id:"$RecordTime","Records":{$push:"$Records"}}},{ $sort:{_id: 1 }})

db.historydata.aggregate({$limit:5},{$match:{"RecordTime":{$gte:"2015-9-10 12:56:35",$lte:"2015-9-10 12:56:38"}}},{$project:{"Records":1,"RecordTime":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':{$in:["4","5"]}}},{$group:{_id:"$RecordTime","Records":{$push:"$Records"}}},{ $sort:{_id: 1 }})

db.historydata.aggregate({$limit:100},{$match:{"RecordTime":{$gte:"2015-9-10",$lte:"2015-9-11"}}},{$project:{"Records":1,"RecordTime":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':{$in:["4","5"]}}},{$group:{_id:"$RecordTime","Records":{$push:"$Records"}}},{ $sort:{_id: 1 }})

db.historydata.aggregate({$limit:100},{$match:{"RecordTime":{$gte:"2015-9-10",$lte:"2015-9-11"}}},{$project:{"Records":1,"RecordTime":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':{$in:["4","5"]}}},{ $sort:{_id: 1 }})

db.historydata.aggregate({$match:{"RecordTime":{$gte:"2015-08-17 10:20:00",$lte:"2015-08-17 13:20:10"}}},{$project:{"Records":1,"RecordTime":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':{$in:["1","2","3"]}}},{$group:{_id:"$RecordTime","Records":{$push:"$Records"}}},{ $group: { _id: "", count: { $sum: 1 } } },{ $sort:{_id: 1 }})

//记录个数
db.historydata.find({"RecordTime":{$gte:"2015-08-17 10:20:00",$lte:"2015-08-17 10:50:10"}}).count()

//分页获取
db.historydata.aggregate({$match:{"RecordTime":{$gte:"2015-08-17 10:20:00",$lte:"2015-08-18 10:20:10"}}},{$skip:10000},{$limit:50},{$project:{"Records":1,"RecordTime":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':{$in:["1","2","3"]}}},{$group:{_id:"$RecordTime","Records":{$push:"$Records"}}},{ $sort:{_id: 1 }})

db.historydata.aggregate({$match:{"RecordTime":{$gte:"2015-08-17 10:20:00",$lte:"2015-08-17 10:50:10"}}},{$skip:500},{$limit:500},{$project:{"Records":1,"RecordTime":1}},{$unwind:"$Records"},{$match:{'Records.SpotID':{$in:["1","2","3"]}}},{$group:{_id:"$RecordTime","Records":{$push:"$Records"}}},{ $sort:{_id: 1 }})

 

 

时间范围:2015-08-22 12:33:29 2021-04-30 21:23:29 测点:0-999

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值