zoukankan      html  css  js  c++  java
  • MongoDB复杂查询小样

    通过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;
        }
  • 相关阅读:
    React生命周期, 兄弟组件之间通信
    React组件式编程Demo-用户的增删改查
    React之this.refs, 实现数据双向绑定
    CCF CSP 201812-4 数据中心
    CCF CSP 201812-4 数据中心
    PAT 顶级 1020 Delete At Most Two Characters (35 分)
    PAT 顶级 1020 Delete At Most Two Characters (35 分)
    Codeforces 1245C Constanze's Machine
    Codeforces 1245C Constanze's Machine
    CCF CSP 201712-4 行车路线
  • 原文地址:https://www.cnblogs.com/s6-b/p/11567792.html
Copyright © 2011-2022 走看看