关于MongoDB的使用

数据格式

 

 

查询

 1 // 查询 集合run_real_time_record下type=2011的所有数据量
 2 db.getCollection("run_real_time_record").find({"type" : "2011"}).count();
 3 
 4 // 只查询集合run_real_time_record对应字段信息(0表示不显示,1表示显示,_id 默认为1)
 5 db.run_real_time_record.aggregate( [ { $project : { _id: 0, type : 1 , monitorPointId : 1 ,content:1 ,terminalId:1} } ] );
 6 
 7 // 或查询 以及分页显示
 8 db.getCollection("run_real_time_record").find(
 9     { $or : [
10         { $and : [{"type" : "2011"}, {"terminalId" : null}] }, 
11         { $and : [{"type" : "2011"}, {"monitorPointId" : null}] }
12     ] }
13 ).limit(1000).skip(0)
14 ;
15 
16 // https://blog.csdn.net/xiangwangxiangwang/article/details/88821513

 

删除

1 // 删除run_real_time_record集合下指定属性的文档
2 db.getCollection("run_real_time_record").remove( { _id: "2347e692d84048a88df124c42bffb99a" }, { justOne: true } )
3 // 删除集合run_real_time_record 下type为2011,content含有"设备"的文档
4 db.getCollection("run_real_time_record").remove( {"type" : "2011" , "content":{$regex:/设备/}} );
5 
6 
7 #参考(有关联删除的,暂时没有试过)
8 // https://blog.csdn.net/xiangwangxiangwang/article/details/88821513
9 // https://blog.csdn.net/T_Mac9334/article/details/102783692

 

视图

1 // 删除视图view_record_2011
2 db.view_record_2011.drop();
3 // 创建视图view_record_2011,type为2011,happenTime倒序
4 db.createView("view_record_2011","run_real_time_record",[{$match:{"type":"2011"}},{$project:{type:1,content:1,happenTime:1,monitorPointId:2,terminalId:1}},{$sort:{"happenTime":-1}}]);

 

posted @ 2022-03-18 17:21  时光之梦  阅读(30)  评论(0编辑  收藏  举报