$ cond运算符java代码

时间:2013-12-16 08:08:42

标签: mongodb mongodb-java aggregation-framework

我有一个聚合查询,如下所示:

db.TWITTER_DATA_Processed.aggregate( {$match : { SpId : 840, Scheduler_id : "SCH_01" }},{$group : {_id : {SpId : "$SpId", Scheduler_id : "$Scheduler_id",Country : "$Country"}, positive_count :  { $sum: { $cond: [ { $gt: [ "$Sentiment", 0 ] } , 1, 0 ] } }, neutral_count :  { $sum: { $cond: [ { $eq: [ "$Sentiment", 0 ] } , 1, 0 ] } }, negative_count :  { $sum: { $cond: [ { $lt: [ "$Sentiment", 0 ] } , 1, 0 ] } }}})

示例输出:

 {
         "_id" : {
                 "SpId" : 840,
                 "Scheduler_id" : "SCH_01",
                 "Country" : "Argentina"
         },
         "positive_count" : 1,
         "neutral_count" : 2,
         "negative_count" : 0
 },
 {
         "_id" : {
                 "SpId" : 840,
                 "Scheduler_id" : "SCH_01",
                 "Country" : "United Kingdom"
         },
         "positive_count" : 17,
         "neutral_count" : 4,
         "negative_count" : 1
 }

由于$ cond运算符,我在编写相同的java代码时遇到了困难。从现有的线程中,我假设$ gt必须写成如下:

DBObject positiveSentiment = new BasicDBObject("$Sentiment", new BasicDBObject("$gt", 0));

我唯一可以弄清楚$ cond的是它需要一个操作数数组(在我的实验中,我得到了以下错误:

"errmsg" : "exception: the $cond operator requires an array of 3 operands" , "code" : 16019

但我如何适合

  

[{$ gt:[“$ Sentiment”,0]},1,0]

在Java代码中?

添加最终的工作代码,以防将来有人需要参考:

DBObject groupIdKeys = new BasicDBObject("SpId", "$SpId");
        groupIdKeys.put("Scheduler_id", "$Scheduler_id");
        groupIdKeys.put("Country", "$Country");

        /* positive sentiments */
        List<Object> gtOperands = new ArrayList<Object>();
        gtOperands.add("$Sentiment");
        gtOperands.add(0);

        List<Object> positiveCondOperands = new ArrayList<Object>();
        positiveCondOperands.add(new BasicDBObject("$gt", gtOperands));
        positiveCondOperands.add(1);
        positiveCondOperands.add(0);
        BasicDBObject finalPositiveCond = new BasicDBObject("$cond",
                positiveCondOperands);

        DBObject sumOfPositiveCount = new BasicDBObject("$sum",
                finalPositiveCond);

        /* neutral sentiments */
        List<Object> eqOperands = new ArrayList<Object>();
        eqOperands.add("$Sentiment");
        eqOperands.add(0);

        List<Object> neutralCondOperands = new ArrayList<Object>();
        neutralCondOperands.add(new BasicDBObject("$eq", eqOperands));
        neutralCondOperands.add(1);
        neutralCondOperands.add(0);
        BasicDBObject finalNeutralCond = new BasicDBObject("$cond",
                neutralCondOperands);

        DBObject sumOfNeutralCount = new BasicDBObject("$sum", finalNeutralCond);

        /* negative sentiments */
        List<Object> ltOperands = new ArrayList<Object>();
        ltOperands.add("$Sentiment");
        ltOperands.add(0);

        List<Object> negativeCondOperands = new ArrayList<Object>();
        negativeCondOperands.add(new BasicDBObject("$lt", ltOperands));
        negativeCondOperands.add(1);
        negativeCondOperands.add(0);
        BasicDBObject finalNegativeCond = new BasicDBObject("$cond",
                negativeCondOperands);

        DBObject sumOfNegativeCount = new BasicDBObject("$sum",
                finalNegativeCond);

        DBObject groupKeys = new BasicDBObject("_id", groupIdKeys);
        groupKeys.put("positive_count", sumOfPositiveCount);
        groupKeys.put("neutral_count", sumOfNeutralCount);
        groupKeys.put("negative_count", sumOfNegativeCount);

        DBObject group = new BasicDBObject("$group", groupKeys);

        AggregationOutput output = dbCollection.aggregate(firstOp, group);

        Iterable<DBObject> results = output.results();

        for (DBObject result : results) {
            System.out.println(result);
        }

1 个答案:

答案 0 :(得分:3)

按照示例here,您可以为“$ cond”运算符创建数组,如下所示:

ArrayList gtA = new ArrayList();
gtA.add("$Sentiment");
gtA.add(0);
ArrayList condArray = new ArrayList();
condArray.add(new BasicDBObject("$gt", gtA));
condArray.add(1);
condArray.add(0);
BasicDBObject fullCond = new BasicDBObject("$cond", condArray);