1

dies meine DatenSum-Aggregationen in MongoDB Mit Spring 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"} 

und mondodb Skript sehr gut laufen. Hier ist 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" } }} 
    ]); 

mein Problem ist, dass ich diesen Code unter Federdaten ausgeführt werden soll

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

bitte geben Sie mir einen Tipp, mein Problem zu lösen .... Dank im Voraus!

+0

i han kippe definieren dle Zustand abgelegt. – james

Antwort

0

Es gibt keine Möglichkeit, dies jetzt mit Feder zu tun. Versuchen Sie stattdessen, DBOject zu verwenden.

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

Irgendwo in Ihrem Projekt erstellen, um diese 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); 
    } 

} 

Statt mit ProjectionOperation verwenden:

AggregationOperation projectStage = projectLikeNY(); 


Aggregation aggregation = Aggregation.newAggregation(matchStage, groupStage,projectStage,sortStage); 

this helps

0

Sie tun können, es auf einen der folgenden Wege.

Mit Criteria definieren eq Operator.

So etwas wie

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 Mit eq Operator

So etwas wie

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

komplette Pipeline

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