this is my 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 very good. here is 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 is that i want to run this 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 a tip to solve my problem.... thanks in advance!
There is no way, as of now, to do it with 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 your project create this 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 this helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.