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

2

0

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?

Hetero answered 27/6, 2013 at 5:17 Comment(1)
Your "project" in the native command doesn't match your "project" in the Java implementation - in Java you're using "hourly_0" as a field and "count" in the native version. You have a similar problem in "group". Which accurately represents your code?Heliotropin
M
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);
Mossbunker answered 27/6, 2013 at 16:27 Comment(0)
O
0

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...

Oratorio answered 18/9, 2013 at 18:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.