在Java中实现MongoDB的Group功能
来源:互联网 发布:js的面试题 编辑:程序博客网 时间:2024/04/29 13:50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* keyColumn : new String[]{"xxxName","xxxType"} <br>
* condition : 查询条件 ,可为空<br>
* initial : 分组统计初始变量,为空时自动为每列提供初始变量<br>
* reduce : 记录处理function<br>
* finalize : finalize函数,可为空 <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
) {
//定义一些初始变量
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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//任务统计
@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;
}
1
[ { "taskStatus" : { "count" : 4.0 , "taskStatus" : "Finished"}} , { "taskStatus" : { "count" : 3.0 , "taskStatus" : "Received"}} , { "taskStatus" : { "count" : 2.0 , "taskStatus" : "UnReceive"}}]
- 在Java中实现MongoDB的Group功能
- Java中实现MongoDB的Group功能
- java中使用containsKey实现的group by功能
- Java 实现MongoDB Group 操作
- java中MongoDB中group的用法与实例
- mongodb mapreduce, aggregate, group 的类似功能
- mongodb中的group功能
- mongodb使用aggregate、group、match实现mysql中的having(count(1)>1)的功能
- mongodb命令行group分组和java代码中group分组
- mongodb命令行group分组和java代码中group分组
- 如何在Mongodb中实现数据超时自动删除功能?
- MongoDB MapReduce实现的group问题
- MongoDB中关于group的用法
- 在JAVA应用程序中如何实现FTP的功能
- 在java中实现sql的select函数功能
- 在Java应用程序中如何实现FTP的功能
- 在JAVA应用程序中如何实现FTP的功能
- 如何在Java程序中实现FTP的上传下载功能
- 消费者问题实现
- 单链表操作java实现
- C#对象和XMl文件之间的转换(转)
- unix网络模型
- DB基本概念之 --- 约束
- 在Java中实现MongoDB的Group功能
- ListView分页加载数据
- 适配ios7界面的方法
- 热插拔USB 设备出现usb 1-1: device descriptor read/64, error -110错误
- 大学期望
- Java实现MongoDB的日期(Date)分组功能
- RAC Public/Private/Vip 区别
- mongoDB 入门指南、示例
- dm9000网卡驱动移植全析