mongodb的聚合查询 java代码 www.2cto.com mongoclient mongoclient = null; try { mongoclient = new mongoclient(102.198.119.29, 27017); db db = mongoclient.getdb(syslog); //设置where条件 dbobject match = new basicdbobject($match, new basicdbob
mongodb的聚合查询
java代码 www.2cto.com
mongoclient mongoclient = null;
try {
mongoclient = new mongoclient(102.198.119.29, 27017);
db db = mongoclient.getdb(syslog);
//设置where条件
dbobject match = new basicdbobject($match, new basicdbobject(
date,
(new basicdbobject($gt, 2012-11-28 00:19:00)).append(
$lt, 2012-11-29 09:19:00)));
//设置分组字段
basicdbobject groupfilters = new basicdbobject(_id, $priority);
//对分组字段统计行数
groupfilters.put(count, new basicdbobject($sum, 1));
basicdbobject group = new basicdbobject($group, groupfilters);
//设置显示的字段集合
dbobject fields = new basicdbobject(priority, 1);
fields.put(_id, $priority);
dbobject project = new basicdbobject($project, fields);
aggregationoutput output = db.getcollection(messages).aggregate(
match, project, group);
system.out.println(output.getcommandresult());
} catch (unknownhostexception e) {
e.printstacktrace();
}