如何定义"$ add:[" $ count" ,"$ count1"]"聚合与mongo-java实现? [英] How to define “$add :["$count" , "$count1"]” aggregation with mongo-java implementation?

查看:252
本文介绍了如何定义"$ add:[" $ count" ,"$ count1"]"聚合与mongo-java实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是示例文档结构.

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") });

我正在尝试编写一个查询,以按照以下输出格式将每小时列的信息按8小时{所有8小时的总和}和月份进行分组.

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}

输出定义为6月{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.

我已经编写了以下本机命令和Java代码来执行此操作.

I have written the below native command and Java code to perform this.

本机命令::

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实现::

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

//现在$ group操作 *

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

*

//运行聚合

AggregationOutput output = table.aggregate(match,group,project);

我几乎已经完成了逻辑,但是$ add命令的唯一问题.如何使用mongo-java实现定义"$ add:["$ count","$ count1"]. 我无法使用Java实现在$ add命令中提及多个值.有人可以建议我如何完成此操作吗?

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?

推荐答案

使用BasicDBList表示Java中的数组. 您的代码将类似于以下内容:

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

这篇关于如何定义"$ add:[" $ count" ,"$ count1"]"聚合与mongo-java实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆