Java code www.2cto. commongoclient?clientnull; try {mongoclientnew?client (102.198.119.29, 27017); dbdb=client. getDB (syslog); set the Where condition Scheme ($ match, newBasicDBOb
Java code www.2cto.com using client = null for MongoDB aggregation query; try {using client = new using client (102.198.119.29, 27017); DB db = using client. getDB (syslog); // set the Where condition DBObject match = new BasicDBObject ($ match, new BasicDBOb
MongoDB Aggregate Query
Java code www.2cto.com
Publish client publish client = null;
Try {
Publish client = new publish client ("102.198.119.29", 27017 );
DB db = logging client. getDB ("syslog ");
// Set the Where Condition
DBObject match = new BasicDBObject ("$ match", new BasicDBObject (
"DATE ",
(New BasicDBObject ("$ gt", "00:19:00"). append (
"$ Lt", "09:19:00 ")));
// Set the grouping Field
BasicDBObject groupFilters = new BasicDBObject ("_ id", "$ PRIORITY ");
// Count the number of rows for the grouping Field
GroupFilters. put ("count", new BasicDBObject ("$ sum", 1 ));
BasicDBObject group = new BasicDBObject ("$ group", groupFilters );
// Set the set of displayed Fields
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 ();
}