How to define “$add :[“$count” , “$count1”]” aggregation with mongo-java implementation?

假装没事ソ 提交于 2019-12-12 03:42:12

问题


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?


回答1:


Use BasicDBList to represent an array in Java. Your code would look similar to this:

    DBObject fields = new BasicDBObject("_id", 1);
        fields.put("hourly_0", 1);
        fields.put("hourly_1", 1);

        BasicDBList dbList = new BasicDBList();
        dbList.add("$hourly_0");
        dbList.add("$hourly_1");
        fields.put("01-08", new BasicDBObject("$add",dbList));

    DBObject project = new BasicDBObject("$project", fields);



回答2:


How to define “$add :["$count" , "$count1"]” with mongo-java implementation

String [] srt_array = {"$count", "$count1"};
BasicDBObject add_obj = BasicDBObject ("$add", srt_array);

This work fine, unless, you want to supply a numeric value in the query...



来源:https://stackoverflow.com/questions/17335037/how-to-define-add-count-count1-aggregation-with-mongo-java-impleme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!