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