首页 / MONGODB / MongoDB复杂查询小样
MongoDB复杂查询小样
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MongoDB复杂查询小样,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4992字,纯文字阅读大概需要8分钟。
内容图文
![MongoDB复杂查询小样](/upload/InfoBanner/zyjiaocheng/890/c5e2702730754ef89aefcd4f47300f0b.jpg)
通过rulerCode分组 统计每组中的总数量 > db.data645.aggregate([{$group: { _id: "$rulerCode", count: { $sum: 1 } }}]) { "_id" : "04000101", "count" : 23188627 } { "_id" : "04000102", "count" : 46055501 } { "_id" : "02800005", "count" : 46010054 } { "_id" : "0205FF00", "count" : 46260039 } { "_id" : "0203FF00", "count" : 46315668 } { "_id" : "0201FF00", "count" : 46144939 } { "_id" : "03110000", "count" : 46055396 } { "_id" : "10000002", "count" : 46126639 } { "_id" : "22000000", "count" : 46128531 } { "_id" : "04800002", "count" : 46180150 } { "_id" : "0101FF00", "count" : 45938536 } { "_id" : "0007FF00", "count" : 45952895 } { "_id" : "0001FF00", "count" : 46299699 } { "_id" : "06000002", "count" : 46080602 } { "_id" : "0003FF00", "count" : 46040239 } { "_id" : "02800002", "count" : 46093662 } { "_id" : "0311000C", "count" : 46006563 } { "_id" : "0002FF00", "count" : 46002216 } { "_id" : "02800004", "count" : 46081499 } { "_id" : "0206FF00", "count" : 45689013 } Type "it" for more > > > it { "_id" : "0004FF00", "count" : 46118865 } { "_id" : "0005FF00", "count" : 45790648 } { "_id" : "04800004", "count" : 46035439 } { "_id" : "23000002", "count" : 46141090 } { "_id" : "04800001", "count" : 23111870 } { "_id" : "0061FF00", "count" : 46074137 } { "_id" : "0006FF00", "count" : 46116865 } { "_id" : "0102FF00", "count" : 45960361 } { "_id" : "10000001", "count" : 23143638 } { "_id" : "04800003", "count" : 46002081 } { "_id" : "0311FF01", "count" : 23037920 } { "_id" : "03100000", "count" : 46159809 } { "_id" : "0204FF00", "count" : 45866787 } { "_id" : "0202FF00", "count" : 45955009 } { "_id" : "0008FF00", "count" : 46431485 } { "_id" : "23000001", "count" : 23083520 } { "_id" : "0060FF00", "count" : 45917357 } > it no cursor 在指定时间范围内 根据rulerCode分组 统计每组的总数量 > db.data645.aggregate( [ ... { $match : { storageTime : { $gte : "2019-09-10 00:00:00", $lt : "2019-09-11 23:59:59" } } }, ... { $group: { _id: "$rulerCode", count: { $sum: 1 } } } ... ] ) { "_id" : "0202FF00", "count" : 80 } { "_id" : "0101FF00", "count" : 80 } { "_id" : "0002FF00", "count" : 80 } { "_id" : "04800004", "count" : 80 } { "_id" : "03100000", "count" : 80 } { "_id" : "0203FF00", "count" : 80 } { "_id" : "22000000", "count" : 160 } { "_id" : "04800002", "count" : 160 } { "_id" : "23000002", "count" : 240 } { "_id" : "02800005", "count" : 80 } { "_id" : "02800004", "count" : 80 } { "_id" : "0005FF00", "count" : 80 } { "_id" : "0004FF00", "count" : 80 } { "_id" : "0001FF00", "count" : 240 } { "_id" : "0311000C", "count" : 240 } { "_id" : "02800002", "count" : 160 } { "_id" : "0003FF00", "count" : 160 } { "_id" : "0007FF00", "count" : 160 } { "_id" : "0205FF00", "count" : 160 } { "_id" : "0008FF00", "count" : 80 } Type "it" for more > it { "_id" : "0102FF00", "count" : 240 } { "_id" : "04000101", "count" : 80 } { "_id" : "0201FF00", "count" : 80 } { "_id" : "04800001", "count" : 80 } { "_id" : "0061FF00", "count" : 160 } { "_id" : "0060FF00", "count" : 80 } > it no cursor > db.data645.findOne() { "_id" : ObjectId("5d639e2446e0fb0001c7b76a"), "_class" : "com.alibaba.fastjson.JSONObject", "dataItem" : "1.662", "terminalTime" : "2019-07-10 16:34:04", "storageTime" : "2019-08-26 16:53:56", "rulerCode" : "02800004", "autoType" : "auto", "ammeterId" : "045925041806" } > > > db.data645.find().count() 1589500037 > db.data645.find({storageTime : { $gte : "2019-09-10 00:00:00", $lt : "2019-09-11 23:59:59" }}).count() 3280
代码实现
@Override public ArrayList<Data645> getListUsedToTest(String maxStorageTime, String now) { ArrayList<Data645> data645ArrayList=null; //封装查询条件 List<AggregationOperation> operations = new ArrayList<>(); try { //从MongoDB读取数据的开始时间 long beginMongoTime=System.currentTimeMillis(); //过滤条件 operations.add(Aggregation.match(Criteria.where("rulerCode").ne("String_89!qaz@wsx").andOperator( Criteria.where("storageTime").gte(maxStorageTime), Criteria.where("storageTime").lt(now) ))); //分组统计 operations.add(Aggregation.group("rulerCode").sum("_id").as("totalCount")); Aggregation aggregation = Aggregation.newAggregation(operations); AggregationResults<Data645> results= mongoTemplate.aggregate(aggregation, "data645", Data645.class); //从MongoDB读取数据的结束时间 long endMongoTime=System.currentTimeMillis(); EtlProjectApplication.loggerMongoDBToMysql.info("step5 ===> MongoDB聚合查询耗时花费 :"+(endMongoTime - beginMongoTime)+" 毫秒"+" 当前时间为 :"+ DateUtil.getStringDate()); System.err.println("step5 ===> MongoDB聚合查询耗时花费 :"+(endMongoTime - beginMongoTime)+" 毫秒"+" 当前时间为 :"+ DateUtil.getStringDate()); // 统计总数量返回 此数量统计用于渲染曲线的x Query query = new Query( Criteria.where("rulerCode").ne("String_89!qaz@wsx") .andOperator( Criteria.where("storageTime").gte(maxStorageTime), Criteria.where("storageTime").lt(now) ) ).with(new Sort(new Sort.Order(Sort.Direction.ASC,"storageTime"))); data645ArrayList=(ArrayList<Data645>) mongoTemplate.find(query,Data645.class); }catch (Exception e){ e.printStackTrace(); } return data645ArrayList; }
内容总结
以上是互联网集市为您收集整理的MongoDB复杂查询小样全部内容,希望文章能够帮你解决MongoDB复杂查询小样所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。