1

这是我的数据总和聚集在MongoDB中使用Spring数据

{"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"} 

和mondodb脚本运行的非常好。这里是代码。

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

我的问题是,我想在春天的数据运行这段代码

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

请给我一个提示,以解决我的问题.... 在此先感谢!

+0

我不能韩dle条件提交。 – james

回答

0

现在还没有办法像春天那样做。尝试使用DBOject。

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

某处在你的项目中创建这种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); 
    } 

} 

除了使用ProjectionOperation使用的:

​​

希望这有助于

0

你能做到以下之一方法。使用Criteria来定义eq运算符。

喜欢的东西

import static org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond; 
import static org.springframework.data.mongodb.core.aggregation.ConditionalOperators.when; 
import static org.springframework.data.mongodb.core.query.Criteria.where; 

Cond likeY = when(where("like").is("Y")).then(1).otherwise(0); 
Cond likeN = when(where("like").is("N")).then(1).otherwise(0); 

使用ComparisonOperators定义eq操作

喜欢的东西

import static org.springframework.data.mongodb.core.aggregation.ComparisonOperators.Eq.valueOf; 

Cond likeY = when(valueOf("like").equalToValue("Y")).then(1).otherwise(0); 
Cond likeN = when(valueOf("like").equalToValue("N")).then(1).otherwise(0); 

完成管道

import static org.springframework.data.domain.Sort.Direction.DESC; 
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; 
/**Insert one of cond imports from above**/ 

MatchOperation matchStage = match(where("delYn").ne("Y")); 
/**Insert one of cond experssions from above**/ 
ProjectionOperation projectStage = project("intentId").and(likeY).as("likeY").and(likeN).as("likeN"); 
GroupOperation groupStage = group("intentId").sum("likeY").as("likeY").sum("likeN").as("likeN"); 
SortOperation sortStage = sort(DESC, "_id"); 
Aggregation aggregation = newAggregation(matchStage, projectStage, groupStage, sortStage);