Below is sample document structure.
db.volume_statistics.insert({ "client": "SER",
"message": "Sample_v02RQ",
"GDS": "SABRE",
"daily": 240000,
"hourly": {"0": 1000, "1": 100, "2": 454, "3":3434, "4":3434, "5":343, ... , ”23”:454 },
"date":ISODate("2013-06-23T04:00:00Z") });
I am trying to write a query to group the information of hourly column by 8 hours{Sum of all 8 hours} and month as per below output format.
{ "Month" : 5 , "01-08" : 26970, "09-17" : 45970}
{ "Month" : 6 , "01-08" : 269712, "09-17" : 56970}
The output defines for the month June{6}, it got 269712 requests within 01-08 hours and 56970 requests for 09-17 hours.
I have written the below native command and Java code to perform this.
Native command ::
db.volume_statistics.aggregate( { $match: { date: { $gt: ISODate("2011-01-01T00:00:00Z") } } },
{ $group : { _id: { month: { $month: "$date" } }, count: { $sum: "$hourly.0" }, count1: { $sum: "$hourly.1" } } },
{ $project: { _id: 1, count : 1 , count1 : 1 , "01-08" :{ $add:["$count", "$count1"]} } } );
Java implementation ::
DBObject match = new BasicDBObject("$match", new BasicDBObject("date",new BasicDBObject("$gt",fromDate)));
DBObject fields = new BasicDBObject("_id", 1);
fields.put("hourly_0", 1);
fields.put("hourly_1", 1);
fields.put("01-08", new BasicDBObject("$add","$hourly_0"));
DBObject project = new BasicDBObject("$project", fields);
// Now the $group operation *
DBObject groupFields = new BasicDBObject("_id", new BasicDBObject("$month","$date"));
groupFields.put("hourly_0", new BasicDBObject("$sum", "$hourly.0"));
groupFields.put("hourly_1", new BasicDBObject("$sum", "$hourly.1"));
DBObject group = new BasicDBObject("$group", groupFields);
*
// run aggregation
AggregationOutput output = table.aggregate(match,group,project);
Almost I have done with the logic but the only issue with $add command. How to define “$add :["$count" , "$count1"]” with mongo-java implementation. I cld not able to mention multiple values in $add command with java implementation. Can someone suggest me how to complete this?