$ cond运算符Java代码 [英] $cond operator java code

查看:80
本文介绍了$ cond运算符Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an aggregation query as follows :

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编写如下:

I'm facing difficulty in writing the java code for the same because of the $cond operator. From the existing threads, I assume the $gt has to be written as follows :

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

我只能弄清楚$ cond是它需要一个操作数数组(在我的实验过程中,出现以下错误:

The only thing I could figure out about $cond was that it requires an operand array(during my experiments, I got the following error :

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

但是我怎么适应

[{$ gt:["$ Sentiment",0]},1,0]

[ { $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);
        }

推荐答案

以下示例在这里,您可以像这样为"$ cond"运算符创建数组:

Following example here you can create the array for "$cond" operator like this:

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

这篇关于$ cond运算符Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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