Home > Database > Mysql Tutorial > MongoDB的聚合查询

MongoDB的聚合查询

WBOY
Release: 2016-06-07 14:53:54
Original
1487 people have browsed it

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();  

}  

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template