gpt4 book ai didi

mongodb - $cond 运算符java代码

转载 作者:可可西里 更新时间:2023-11-01 09:13:41 24 4
gpt4 key购买 nike

我有一个聚合查询如下:

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

最佳答案

以下示例 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);

关于mongodb - $cond 运算符java代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20606089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com