首頁 >資料庫 >mysql教程 >MongoDB的聚合查询

MongoDB的聚合查询

WBOY
WBOY原創
2016-06-07 14:53:541485瀏覽

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

}  

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn