Implement the MongoDB Group function in Java

Source: Internet
Author: User
Tags install mongodb

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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.