java - Sum Aggregations in MongoDB Using Spring Data -


this data

{"intentid" : "a1", "like" : "y"} {"intentid" : "a1", "like" : "y"} {"intentid" : "a1", "like" : "n"} {"intentid" : "a2", "like" : "y"} {"intentid" : "a2", "like" : "n"} {"intentid" : "a2", "like" : "n"} {"intentid" : "a2", "like" : "n"} 

and mondodb script run good. here code.

db.getcollection('test').aggregate( [ {          $project:            {              intentid: 1,              likey:                {                  $cond: [ { $eq: [ "$like", "y" ] }, 1, 0 ]                },                liken:                {                  $cond: [ { $eq: [ "$like", "n" ] }, 1, 0 ]                }            }       },      { $group : { _id : "$intentid", likey: { $sum: "$likey" }, liken:  { $sum: "$liken" } }}    ] ); 

my problem want run code under spring data

matchoperation matchstage = aggregation.match(new criteria  ("delyn").ne("y")); groupoperation groupstage = aggregation.group("intentid"); cond operatornbs = conditionaloperators.when("like").then("y").otherwise(value) projectionoperation projectstage = aggregation.p sortoperation sortstage = aggregation.sort(sort.direction.desc, "_id"); aggregation aggregation   = aggregation.newaggregation(matchstage, groupstage,projectstage,sortstage); 

please give me tip solve problem.... in advance!

there no way, of now, spring. try using dboject instead.

public static aggregationoperation projectlikeny() {     dbobject projectyn = new basicdbobject("$project", new basicdbobject("intentid", 1).append("likey", new basicdbobject("$cond", arrays. < object > aslist(new basicdbobject("$eq", arrays. < object > aslist("$like", "y")),             1, 0))).append("liken", new basicdbobject("$cond", arrays. < object > aslist(new basicdbobject("$eq", arrays. < object > aslist("$like", "n")),             1, 0)))      );      customaggregationoperation project= new customaggregationoperation(         projectyn);     return project; } 

somewhere in project create customaggregationoperation:

public class customaggregationoperation implements aggregationoperation {      private dbobject operation;      public customaggregationoperation(dbobject operation) {         this.operation = operation;     }      @override     public dbobject todbobject(aggregationoperationcontext context) {         return context.getmappedobject(operation);     }  } 

instead of using projectionoperation use this:

aggregationoperation projectstage = projectlikeny();   aggregation aggregation   = aggregation.newaggregation(matchstage, groupstage,projectstage,sortstage); 

hope helps


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -