Module spring.data.mongodb
Class AggregationUpdate
java.lang.Object
org.springframework.data.mongodb.core.aggregation.Aggregation
org.springframework.data.mongodb.core.aggregation.AggregationUpdate
- All Implemented Interfaces:
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFluent API AggregationUpdate builder.Nested classes/interfaces inherited from interface org.springframework.data.mongodb.core.query.UpdateDefinition
UpdateDefinition.ArrayFilter -
Field Summary
Fields inherited from class org.springframework.data.mongodb.core.aggregation.Aggregation
CURRENT, DEFAULT_CONTEXT, DEFAULT_OPTIONS, pipeline, REMOVE, ROOT -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreate newAggregationUpdate.protectedAggregationUpdate(List<AggregationOperation> pipeline) Create newAggregationUpdatewith the given aggregation pipeline to apply. -
Method Summary
Modifier and TypeMethodDescriptionstatic AggregationUpdatefrom(List<AggregationOperation> pipeline) Create a new AggregationUpdate from the givenAggregationOperations.Get the specification which elements to modify in an array field.org.bson.DocumentvoidIncrement the value of a given key by1.If true prevents a write operation that affects multiple documents from yielding to other reads or writes once the first document is written.isolated()Prevents a write operation that affects multiple documents from yielding to other reads or writes once the first document is written.booleanCheck if a given key is modified by applying the update.replaceWith(Object value) $replaceWithreplaces the input document with the value.replaceWith(ReplaceWithOperation replaceWithOperation) $replaceWithreplaces the input document with the specified document.Fluent API variant for$setadding a singlepipeline operationevery time.set(SetOperation setOperation) Adds new fields to documents.toString()Short forunset(UnsetOperation).unset(UnsetOperation unsetOperation) $unsetremoves/excludes fields from documents.static AggregationUpdateupdate()Start defining the update pipeline to execute.Methods inherited from class org.springframework.data.mongodb.core.aggregation.Aggregation
addFields, asAggregationList, bind, bucket, bucket, bucketAuto, bucketAuto, count, facet, facet, fields, geoNear, getOptions, getPipeline, graphLookup, group, group, limit, lookup, lookup, lookup, match, match, match, merge, newAggregation, newAggregation, newAggregation, newAggregation, newAggregationOptions, newUpdate, out, previousOperation, project, project, project, redact, replaceRoot, replaceRoot, replaceRoot, sample, skip, sort, sort, sortByCount, sortByCount, stage, stage, toDocument, toPipeline, unwind, unwind, unwind, unwind, withOptionsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.springframework.data.mongodb.core.query.UpdateDefinition
hasArrayFilters
-
Constructor Details
-
AggregationUpdate
protected AggregationUpdate()Create newAggregationUpdate. -
AggregationUpdate
Create newAggregationUpdatewith the given aggregation pipeline to apply.- Parameters:
pipeline- must not be null.
-
-
Method Details
-
update
Start defining the update pipeline to execute.- Returns:
- new instance of
AggregationUpdate.
-
from
Create a new AggregationUpdate from the givenAggregationOperations.- Returns:
- new instance of
AggregationUpdate.
-
set
Adds new fields to documents.$setoutputs documents that contain all existing fields from the input documents and newly added fields.- Parameters:
setOperation- must not be null.- Returns:
- this.
- See Also:
-
unset
$unsetremoves/excludes fields from documents.- Parameters:
unsetOperation- must not be null.- Returns:
- this.
- See Also:
-
replaceWith
$replaceWithreplaces the input document with the specified document. The operation replaces all existing fields in the input document, including the _id field.- Parameters:
replaceWithOperation- must not be null.- Returns:
- this.
- See Also:
-
replaceWith
$replaceWithreplaces the input document with the value.- Parameters:
value- must not be null.- Returns:
- this.
-
set
Fluent API variant for$setadding a singlepipeline operationevery time. To update multiple fields within oneSetOperationuseset(SetOperation).- Parameters:
key- must not be null.- Returns:
- new instance of
AggregationUpdate.SetValueAppender. - See Also:
-
unset
Short forunset(UnsetOperation).- Parameters:
keys- the fields to remove.- Returns:
- this.
-
isolated
Prevents a write operation that affects multiple documents from yielding to other reads or writes once the first document is written.
Use withMongoOperations.updateMulti(Query, UpdateDefinition, Class).- Returns:
- never null.
-
isIsolated
Description copied from interface:UpdateDefinitionIf true prevents a write operation that affects multiple documents from yielding to other reads or writes once the first document is written.- Specified by:
isIsolatedin interfaceUpdateDefinition- Returns:
- true if update isolated is set.
-
getUpdateObject
public org.bson.Document getUpdateObject()- Specified by:
getUpdateObjectin interfaceUpdateDefinition- Returns:
- the actual update in its native
Documentformat. Never null.
-
modifies
Description copied from interface:UpdateDefinitionCheck if a given key is modified by applying the update.- Specified by:
modifiesin interfaceUpdateDefinition- Parameters:
key- must not be null.- Returns:
- true if the actual
UpdateDefinitionattempts to modify the given key.
-
inc
Description copied from interface:UpdateDefinitionIncrement the value of a given key by1.- Specified by:
incin interfaceUpdateDefinition- Parameters:
key- must not be null.
-
getArrayFilters
Description copied from interface:UpdateDefinitionGet the specification which elements to modify in an array field.UpdateDefinition.ArrayFilterare passed directly to the driver without further type or field mapping.- Specified by:
getArrayFiltersin interfaceUpdateDefinition- Returns:
- never null.
-
toString
- Overrides:
toStringin classAggregation
-