2017-07-10 2 views
1

Daten in mongo:Wie schreibe ich im Frühjahr Mongo-Aggregation reduzieren Abfrage?

enter image description here

db.test2.aggregate([ 
    { 
     "$project" : { 
     "contents" : 1, 
     "comments" : { 
      "$filter" : { 
       "input" : "$comments", 
       "as" : "item", 
       "cond" : {"$gt" : ['$$item.score', 2]} 

       }, 
      }, 
     "comments2" : { 
      "$filter" : { 
       "input" : "$comments2", 
       "as" : "item", 
       "cond" : {"$gt" : ["$$item.score", 5]} 
      } 
      } 
     } 
    }, 
    { 
    "$project" : { 
      "content" : 1, 
      "commentsTotal" : { 
       "$reduce" : { 
        "input" : "$comments", 
        "initialValue" : 0, 
        "in" : {"$add" : ["$$value", "$$this.score"]} 
       } 
       }, 
      "comments2Total" : { 
       "$reduce" : { 
       "input" : "$comments2", 
       "initialValue" : 0, 
        "in" : {"$add" : ["$$value", "$$this.score"]} 
       } 
      } 
     } 
     }, 
     {$skip : 0}, 
     {$limit: 3} 

]);

So können Sie sehen, dies macht folgendes: 1, filtern Sie die Kommentare und Kommentare2 die gt Tor gegangen 5. 2, zählen insgesamt der socre in Kommentar-Array.

und ich schreibe die Aggregation Abfrage im Frühjahr wie folgt aus:

AggregationExpression reduce = ArithmeticOperators.Add.valueOf("$$value").add("$$this.socre"); 
    Aggregation aggregation = Aggregation.newAggregation(
      Aggregation.project().andExclude("_id") 
        .andInclude("content") 
        .and("comments").filter("item", ComparisonOperators.Gt.valueOf("item.score").greaterThanValue(3)).as("comments") 
        .and("comments2").filter("item", ComparisonOperators.Gt.valueOf("item.score").greaterThanValue(3)).as("comments2"), 
      Aggregation.project("comments", "comments2") 
        .and(ArrayOperators.Reduce.arrayOf("comments").withInitialValue("0").reduce(reduce)).as("commentsTotal") 
    ); 

wenn ich laufe wie oben, wird es wirft Ausnahme:

java.lang.IllegalArgumentException: Ungültige Referenz '$$ Wert' !

+0

Sieht aus wie zwei Projektphase dieses Verhalten verursacht. Wir können dieses Problem leicht umgehen, wenn Sie nur die Anzahl als endgültige Ausgabe benötigen, wie in Ihrer Shell-Abfrage angezeigt. – Veeram

+0

Wie kann ich jetzt tun? –

Antwort

0

Sie können versuchen, unter Aggregation $filter innerhalb der $reduce Operation.

So etwas wie unten

AggregationExpression reduce1 = new AggregationExpression() { 
     @Override 
     public DBObject toDbObject(AggregationOperationContext aggregationOperationContext) { 
     DBObject filter = new BasicDBObject("$filter", new BasicDBObject("input", "$comments").append("as", "item").append("cond", 
        new BasicDBObject("$gt", Arrays.<Object>asList("$$item.score", 2)))); 
     DBObject reduce = new BasicDBObject("input", filter).append("initialValue", 0).append("in", new BasicDBObject("$add", Arrays.asList("$$value", "$$this.socre"))); 
     return new BasicDBObject("$reduce", reduce); 
    } 
}; 

Aggregation aggregation = newAggregation(
    Aggregation.project().andExclude("_id") 
     .andInclude("content") 
     .and(reduce1).as("commentsTotal") 
); 
+0

Ich versuche es so: AggregationExpression reduce = ArithmeticOperators.Add.valueOf ("$$ value"). Add ("$$ this.socre"); Aggregation Aggregation = Aggregation.newAggregation ( \t \t Aggregation.project() \t \t \t \t .andInclude ("content") \t \t \t \t .und (ArrayOperators.Reduce.arrayOf ( \t \t \t \t \t \t ArrayOperators .Filter.filter ("Kommentare"). As ("Element"). Von (ComparisonOperators.Gt.valueOf ("item.score"). GreaterThanValue (2))). \t \t \t \t \t \t withInitialValue (0) .reduce (reduzieren)). As ("commentsTotal") ); –

+0

Ausnahme: org.springframework.dao.InvalidDataAccessApiUsageException: Befehlsausführung fehlgeschlagen: Fehler [Verwendung undefinierter Variable: Wert], Befehl = {"Aggregat": "Test2", "Pipeline": [{"$ Projekt": {" Inhalt ": 1," commentsTotal ": {" $ reduce ": {" Eingabe ": {" $ add ": [" $$ Wert "," $$ this.socre "]}," initialValue ": 0," in ": {" $ add ": [" $$ Wert "," $$ this.socre "]}}}}}]}; Verschachtelte Ausnahme ist com.mongodb.CommandFailureException: {"serverUsed": "git.ivymei.com:27017", "ok": 0.0, "errmsg": "Verwendung von undefinierter Variable: value", "code": 17276, " codeName ":" Location17276 "} –

+0

Entschuldigung, es sieht so aus, als ob die Verkettungsaggregationsoperatoren nicht wie erwartet funktionieren. Ich habe mit der anderen Lösung ersetzt. – Veeram

Verwandte Themen