2016-07-30 9 views
1

Ich versuche, eine Aggregationspipeline mit Spring Data MongoDB zu erstellen, die ein neues Array-Feld in die Pipeline projiziert. Wie kann ich dies mithilfe von Spring Data erreichen?Wie projiziere ich ein Array-Feld mit Spring-Daten

Die Pipeline-Stufe I zu replizieren versuchen ist wie folgt:

{ 
    $project: { 
     "aceId": 1,  
     "startActivityDateTime": 1, 
     "lastActivityDateTime": 1, 
     "eventInfo": [ 
      "$applicationInfo", 
      "$riskAssessmentInfo", 
      "$policyInfo", 
      "$submissionInfo" 
     ] 
    } 
}, 

Antwort

1

Ich hatte das gleiche Problem und eine Lösung gefunden. Sie haben eine CustomAggregationOperation Klasse zu erstellen, wie es in dieser Antwort vorgeschlagen: https://stackoverflow.com/a/29186539/5033846

public class CustomProjectAggregationOperation implements AggregationOperation { 
    private DBObject operation; 

    public CustomProjectAggregationOperation (DBObject operation) { 
     this.operation = operation; 
    } 

    @Override 
    public DBObject toDBObject(AggregationOperationContext context) { 
     return context.getMappedObject(operation); 
    } 

}

Dann können Sie Ihre Projektionsstufe erreichen Sie wie folgt vor:

new CustomAggregationOperation(new BasicDBObject("$project", 
    new BasicDBObject("aceId", 1) 
     .appending("startActivityDateTime", 1) 
     .appending("lastActivityDateTime", 1) 
     .appending("eventInfo", 
     new Object[]{ 
      "$applicationInfo", 
      "$riskAssessmentInfo", 
      "$policyInfo", 
      "$submissionInfo"} 
     ) 
)); 
Verwandte Themen