Class AggregationUpdate

java.lang.Object
org.springframework.data.mongodb.core.aggregation.Aggregation
org.springframework.data.mongodb.core.aggregation.AggregationUpdate
All Implemented Interfaces:
UpdateDefinition

public class AggregationUpdate extends Aggregation implements UpdateDefinition
Abstraction for db.collection.update() using an aggregation pipeline. Aggregation pipeline updates use a more expressive update statement expressing conditional updates based on current field values or updating one field using the value of another field(s).
 AggregationUpdate update = AggregationUpdate.update().set("average")
                .toValue(ArithmeticOperators.valueOf("tests").avg()).set("grade")
                .toValue(ConditionalOperators
                                .switchCases(CaseOperator.when(Gte.valueOf("average").greaterThanEqualToValue(90)).then("A"),
                                                CaseOperator.when(Gte.valueOf("average").greaterThanEqualToValue(80)).then("B"),
                                                CaseOperator.when(Gte.valueOf("average").greaterThanEqualToValue(70)).then("C"),
                                                CaseOperator.when(Gte.valueOf("average").greaterThanEqualToValue(60)).then("D"))
                                .defaultTo("F"));
 
The above sample is equivalent to the JSON update statement:
 db.collection.update(
    { },
    [
      { $set: { average : { $avg: "$tests" } } },
      { $set: { grade: { $switch: {
                            branches: [
                                { case: { $gte: [ "$average", 90 ] }, then: "A" },
                                { case: { $gte: [ "$average", 80 ] }, then: "B" },
                                { case: { $gte: [ "$average", 70 ] }, then: "C" },
                                { case: { $gte: [ "$average", 60 ] }, then: "D" }
                            ],
                            default: "F"
      } } } }
    ],
    { multi: true }
 )
 
Since:
3.0
Author:
Christoph Strobl, Mark Paluch
See Also: