MongoDB aggregate function application example, mongodbaggregate
Preface
Recently, the company used MongoDB as the database storage and learned how to use MongoDB. As a representative of non-relational databases, the database is mainly stored in document format and has flexible fields, which many developers like.
I feel very convenient after using MongoDB for a day. The program does not need to define entity beans, and the fields are flexible and can be more or less. It is suitable for Internet application development that requires massive storage, high concurrency, and weak transactions for convenient development.
Today, we summarize the application of MongoDB's aggregate function, which is mainly used for data record analysis, such as sum, average, maximum and minimum values, paging, sorting, and other operations, facilitating data query and statistics.
Requirement
The development language is java.
The project requires statistics on the number of reading times, maximum reading time, and average reading time of the article.
The database document field is:
{"_ Id": "1234567", "_ class": "com. opinion. bean. qqBean "," groupName ":" Elementary School Group "," qqNumber ": 127," qqContext ":" One sentence "," period ": 0 }{" _ id ": "1234568", "_ class": "com. opinion. bean. qqBean "," groupName ":" high school group "," qqNumber ": 128," qqContext ":" Two sentences "," period ": 1 }{" _ id ": "1234569", "_ class": "com. opinion. bean. qqBean "," groupName ":" "," qqNumber ": 129," qqContext ":" Two sentences "," period ": 1}
Application:
Public ResultBean getArticleRecordT () {DBObject query = new BasicDBObject (). append ("_ class", "com. opinion. bean. qqBean "); // The int readCount = MongoTemplet read the article according to the Article id and group id. getCollection ("qqBean "). find (query ). count (); // defines the matching function using $ match DBObject match = new BasicDBObject (). append ("$ match", query); // here _ id DBObject total = new BasicDBObject ("_ id", "total"); total. put ("sum", new BasicDBObject ("$ sum", "$ qqNumber"); total. put ("avg", new BasicDBObject ("$ avg", "$ qqNumber"); total. put ("max", new BasicDBObject ("$ max", "$ qqNumber"); // defines the statistical function using $ group DBObject groupFields = new BasicDBObject (). append ("$ group", total); // The aggregate parameter is a series of conditions. getCollection ("qqBean "). aggregate (match, groupFields); // The returned result is a collection List.
Iterator = (List
) Output. results (); for (DBObject dbObject: iterator) {System. out. println (dbObject. get ("sum"); System. out. println (dbObject. get ("avg");} Map
PeriodMap = new LinkedHashMap <> (); int period; for (int I = 0; I <= 24; I = I + 3) {query. put ("period", I); period = valid templet. getCollection ("qqBean "). find (query ). count (); periodMap. put ("perid" + I, period);} Map
Map = new HashMap <> (); map. put ("qq", "1111"); map. put ("periodMap", periodMap); return new ResultBean (map );}
In this way, the sum of statistics, average, and maximum values can be obtained.
Result: