mongoDB aggregate aggregation analysis

Introduction

Aggregation operations are actually used for statistical analysis of data. Simply put, they can be understood as aggregation operations in SQL . Aggregation operations in MongoDB are prepared for big data analysis. Here is a brief introduction to the use of the aggregation framework aggregate .

Pipeline Mode Aggregation Analysis

MongoDB 's aggregation framework is implemented with reference to pipeline commands on UNIX . Data passes through a multi-step pipeline, each step processes the data, and finally returns the required result set.  
Pipeline aggregation can operate on a fragmented collection ( The aggregation pipeline can operate on a sharded collection. )  
Pipeline aggregation can use indexes to improve performance at certain stages. In addition, pipeline aggregation has an internal optimization stage (later pipeline aggregation Optimization will be discussed).

Common pipeline operators are as follows:

operator

illustrate

$match

filter documents

$limit

Limit the data of the files in the pipeline

$skip

skip the specified number of documents

$sort

Sort the entered documents

$group

Calculate aggregated results after grouping documents

$out

Output documents to a specific collection (must be the last step of the pipeline operation)

Aggregation operators used with $ group :

operator

illustrate

$first

Returns the first value after group

$last

Returns the last value after group

$max

The maximum value after group

$min

The minimum value after group

$avg

average after group

$sum

sum after group

Example:

db.Collection.aggregate(

        {$match : {"appId" : "2e1800b22ae70600", "leaveTime" : {"$gt" : ISODate("2017-07-12T00:00:00"), "$lt" : ISODate("2017-07-13T00:00:00")}}},

        {$group : {"_id" : "$leaveMethod", "count" : {$sum : 1}}},

        {$sort : {"_id" : 1}}

  )

Integrate into java operation

First import mongodb 's java driver package mongo-java-driver-3.2.2.jar

Use the new Document() object to replace the above {"XXX": "XXX"} condition

Example:

public String aggregateLeaveMethodByDate(String app_id, Date beginDate, Date endDate) throws Exception {

              MongoCollection<Document> collection = PluginMongo.instance().getDatabase().getCollection(MongoCollectionName.PARKING_RECORD);

              Document sub_match = new Document();

              sub_match.put("appId", app_id);

              sub_match.put("leaveTime", new Document("$gt", beginDate).append("$lt", endDate));

             

              Document sub_group = new Document();

              sub_group.put("_id", "$leaveMethod");

              sub_group.put("count", new Document("$sum", 1));

             

              Document match = new Document("$match", sub_match);

              Document group = new Document("$group", sub_group);

              Document sort = new Document("$sort", new Document("_id", 1));

             

              List<Document> aggregateList = new ArrayList<Document>();

              aggregateList.add(match);

              aggregateList.add(group);

              aggregateList.add(sort);

             

              JSONObject ret_obj = new JSONObject();

              AggregateIterable<Document> resultset = collection.aggregate(aggregateList);

              MongoCursor<Document> cursor = resultset.iterator();

             

              try {

                     while(cursor.hasNext()) {

                            Document item_doc = cursor.next();

                            int leaveMethod = item_doc.getInteger("_id", 0);

                            int count = item_doc.getInteger("count", 0);

                           

                            LeaveMethodEnum leaveMethodVal = LeaveMethodEnum.fromType(leaveMethod);

                            ret_obj.put(leaveMethodVal.name(), count);

                     }

              } finally {

                     cursor.close();

              }

             

              return ret_obj.toJSONString();

       }

Guess you like

Origin blog.csdn.net/caicai250/article/details/81236888