In non-relational database MongoDB, there is no ready-made framework that can easily implement database grouping through Group By like SQL. If we want to implement the MongoDB Group function, we must write the original NO-SQL query statement to achieve the Group function, which is time-consuming and laborious, and easy to error, this article provides an encapsulated interface to implement the MongoDB Group function, so that programmers no longer worry about the MongoDB Group.
The interface method is as follows:
/**
* KeyColumn: new String [] {"xxxName", "xxxType"} <br>
* Condition: Query condition, which can be null <br>
* Initial: Group counts the initial variables. If it is null, the initial variables are automatically provided for each column. <br>
* Reduce: record processing function <br>
* Finalize: finalize function, which can be empty <br>
*/
Public BasicDBList group (String [] keyColumn, DBObject condition,
DBObject initial, String reduce, String finalize ){
DBCollection coll = getCollection ();
DBObject key = new BasicDBObject ();
For (int I = 0; I <keyColumn. length; I ++ ){
Key. put (keyColumn [I], true );
}
Condition = (condition = null )? New BasicDBObject (): condition;
If (StringUtils. isEmpty (finalize )){
Finalize = null;
}
If (initial = null) {// defines some initial variables
Initial = new BasicDBObject ();
For (int I = 0; I <keyColumn. length; I ++ ){
DBObject index = new BasicDBObject ();
Index. put ("count", 0 );
Index. put ("sum", 0 );
Index. put ("max", 0 );
Index. put ("min", 0 );
Index. put ("avg", 0 );
Index. put ("self ","");
Initial. put (keyColumn [I], index );
}
}
BasicDBList resultList = (BasicDBList) coll. group (key, condition,
Initial, reduce, finalize );
Return resultList;
}
Implementation case:
// Task statistics
@ Action (value = "getTaskStatistic", results = {@ Result (name = "success", type = "json", params = {
"IncludeProperties", "jsonResult "})})
Public String getTaskStatistic (){
DBObject initial = new BasicDBObject ();
DBObject index = new BasicDBObject ();
Index. put ("count", 0 );
Index. put ("taskStatus ","");
Initial. put ("taskStatus", index );
String reduce = "function (doc, out ){"
+ "Out. taskStatus. count = out. taskStatus. count + = 1 ;"
+ "Out. taskStatus. inspectStatus = doc. taskStatus ;"
+ "}";
BasicDBList group = (BasicDBList) taskStatusService. group (new String [] {"taskStatus"}, null, initial, reduce, null );
This. jsonResult = group. toString ();
Return SUCCESS;
}
Returned data:
[{"TaskStatus": {"count": 4.0, "taskStatus": "Finished" }}, {"taskStatus": {"count": 3.0, "taskStatus ": "Received" }},{ "taskStatus": {"count": 2.0, "taskStatus": "UnReceive"}]
MongoDB details: click here
MongoDB: click here
Recommended reading:
Java-based self-growth field in MongoDB
CentOS compilation and installation of MongoDB
CentOS compilation and installation of php extensions for MongoDB and mongoDB
CentOS 6 install MongoDB and server configuration using yum
Install MongoDB2.4.3 in Ubuntu 13.04
How to create a new database and set in MongoDB
MongoDB beginners must read (both concepts and practices)
MongoDB authoritative Guide (The Definitive Guide) in English [PDF]