问题
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 ] } }}})
Sample output :
{
"_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
}
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));
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
But how do I fit
[ { $gt: [ "$Sentiment", 0 ] } , 1, 0 ]
in the Java code ?
Adding the final working code, just in case anyone needs in future for reference :
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:
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);
来源:https://stackoverflow.com/questions/20606089/cond-operator-java-code