如何在mongo java中实现$ group聚合代码

时间:2014-01-23 07:56:03

标签: java mongodb aggregation-framework

我想在Java中使用mongo聚合,但我不能将$group代码定义为:

在javascript中:

$group = {
        '_id':null,
        'money_bank':{
            '$sum':{
                '$cond':[{'$eq'=>{'$type':'bank'}},'$amount',0]
            }
          }

在java中:

BasicDBList eqList = new BasicDBList();
eqList.add("$type");
eqList.add("bank");
DBObject eqObject = BasicDBObjectBuilder.start().add("$eq", eqList).get();

BasicDBList condList = new BasicDBList();
condList.add(eqObject);
condList.add("$amount");
condList.add(0);
DBObject conObj = BasicDBObjectBuilder.start().add("$cond", condList).get();
DBObject sumObj = BasicDBObjectBuilder.start().add("$sum", conObj).get();
DBObject moneyObj = BasicDBObjectBuilder.start().add("money_bank", sumObj).get();
DBObject idObj = BasicDBObjectBuilder.start().add("_id", null).get();
BasicDBList groupList = new BasicDBList();
groupList.add(idObj);
groupList.add(moneyObj);
DBObject group = new BasicDBObject("$group", groupList);

但是当我执行代码时,错误:

"errmsg" : "exception: a group's fields must be specified in an object" 

请用Java帮助我。

1 个答案:

答案 0 :(得分:0)

您有点误解了将其转换为Java调用的正确方法,您使用BasicDBList的两个地方实际上需要使用BasicDBObject - 这就是错误意味着,这意味着你需要使用一个不是列表的对象。

我已对代码进行了更改,但我没有针对MongoDB进行测试,我将其留给您。

为您提供相当于Javascript所需的最小更改是:

BasicDBObject eqArgs = new BasicDBObject("$type", "bank");
DBObject eqObject = BasicDBObjectBuilder.start().add("$eq", eqArgs).get();

BasicDBList condList = new BasicDBList();
condList.add(eqObject);
condList.add("$amount");
condList.add(0);
DBObject conObj = BasicDBObjectBuilder.start().add("$cond", condList).get();
DBObject sumObj = BasicDBObjectBuilder.start().add("$sum", conObj).get();
BasicDBObject groupArgs = new BasicDBObject();
groupArgs.append("_id", null);
groupArgs.append("money_bank", sumObj);
DBObject group = new BasicDBObject("$group", groupArgs);

// Using an assert to check it's correct, remove in production code
Assert.assertThat(group.toString(), is("{ \"$group\" : { \"_id\" :  null  , " +
                                                        "\"money_bank\" : { " +
                                                            "\"$sum\" : { " +
                                                                "\"$cond\" : [ { \"$eq\" : { \"$type\" : \"bank\"}} , " +
                                                                "\"$amount\" , 0]" +
                                                            "}" +
                                                        "}" +
                                                       "}" +
                                       "}"));

但是你可以进一步简化它(虽然我承认Java仍然比JavaScript更丑):

BasicDBList condList = new BasicDBList();
condList.add(new BasicDBObject("$eq", new BasicDBObject("$type", "bank")));
condList.add("$amount");
condList.add(0);
DBObject sumObj = new BasicDBObject("$sum", new BasicDBObject("$cond", condList));
DBObject group = new BasicDBObject("$group", new BasicDBObject("_id", null).append("money_bank", sumObj));

由于某种原因,BasicDBObjectBuilder比使用BasicDBObject更加冗长。