MongoDB complex queries demo

Statistics by the total number of packets in each group rulerCode
 > db.data645.aggregate ([{$ Group: {the _id: " $ rulerCode " , COUNT: {$ SUM: . 1 }}}]) 


{ " the _id " : " 04,000,101 " , " COUNT " : 23,188,627 } 
{ " the _id " : " 04,000,102 " , " COUNT " : 46,055,501 } 
{ " the _id " : " 02,800,005 " ,"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": db.data645.aggregate ([" 0008FF00 " , " COUNT " : 46,431,485 } 
{ " the _id " : " 23,000,001 " , " COUNT " : 23.08352 million } 
{ " the _id " : " 0060FF00 " , " COUNT " : 45,917,357 }
 > IT 
NO Cursor 
according to the specified time range the total number of each group statistics rulerCode
 > 
... {$ 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

Code

@Override
     public the ArrayList <Data645> getListUsedToTest (maxStorageTime String, String now) { 
        the ArrayList <Data645> data645ArrayList = null ;
         // package query 
        List <AggregationOperation> Operations = new new the ArrayList <> ();
         the try {
             // read from MongoDB start time of the data 
            Long beginMongoTime = System.currentTimeMillis ();
             // filter condition 
            operations.add (Aggregation.match (Criteria.where ( "rulerCode") NE ( "String_89 WSX qaz @!") andOperator (.. 
                    Criteria.where ( "storageTime"). gte ( maxStorageTime),
                    . Criteria.where ( "storageTime") lt (now) lt(now)
            )));
            // packet statistics 
            operations.add (Aggregation.group ( "rulerCode") SUM ( "_ ID") AS ( "The totalCount")..); 
            Aggregation Aggregation = aggregation. newAggregation (Operations); 
            AggregationResults <Data645> Results = mongoTemplate.aggregate (aggregation, "data645", Data645.class); 
            // from the end time of reading data MongoDB 
            Long endMongoTime = System.currentTimeMillis (); 
            EtlProjectApplication.loggerMongoDBToMysql.info ( " STEP5 ===> Processed aggregate queries MongoDB cost: " + (endMongoTime - beginMongoTime) + " milliseconds "+ "   Current time: "+ DateUtil.getStringDate ());
            System.err.println ( " STEP5 ===> MongoDB aggregate queries take time-consuming: " + (endMongoTime - beginMongoTime) + " milliseconds " + "   Current time: " + DateUtil.getStringDate ()); 

            // count the total number of returns the number of statistics for rendering curves of the X- 
            Query Query = new new Query ( 
                    Criteria. the WHERE ( " rulerCode " ) .ne ( " String_89! qaz @ WSX " ) 
                            .andOperator ( 
                                    Criteria. the 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;
    }

 

Guess you like

Origin www.cnblogs.com/s6-b/p/11567792.html