MongoDB的聚合查询
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();
}
上一篇: 3款值得推荐的微信开发开源框架_PHP
下一篇: 一段算法的实现,嵌套判断