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
Modifier and TypeClassDescriptionstatic interface
Fluent 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
ModifierConstructorDescriptionprotected
Create newAggregationUpdate
.protected
AggregationUpdate
(List<AggregationOperation> pipeline) Create newAggregationUpdate
with the given aggregation pipeline to apply. -
Method Summary
Modifier and TypeMethodDescriptionstatic AggregationUpdate
from
(List<AggregationOperation> pipeline) Create a new AggregationUpdate from the givenAggregationOperation
s.Get the specification which elements to modify in an array field.org.bson.Document
void
Increment 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.boolean
Check if a given key is modified by applying the update.replaceWith
(Object value) $replaceWith
replaces the input document with the value.replaceWith
(ReplaceWithOperation replaceWithOperation) $replaceWith
replaces the input document with the specified document.Fluent API variant for$set
adding a singlepipeline operation
every time.set
(SetOperation setOperation) Adds new fields to documents.toString()
Short forunset(UnsetOperation)
.unset
(UnsetOperation unsetOperation) $unset
removes/excludes fields from documents.static AggregationUpdate
update()
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, withOptions
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.springframework.data.mongodb.core.query.UpdateDefinition
hasArrayFilters
-
Constructor Details
-
AggregationUpdate
protected AggregationUpdate()Create newAggregationUpdate
. -
AggregationUpdate
Create newAggregationUpdate
with 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 givenAggregationOperation
s.- Returns:
- new instance of
AggregationUpdate
.
-
set
Adds new fields to documents.$set
outputs 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
$unset
removes/excludes fields from documents.- Parameters:
unsetOperation
- must not be null.- Returns:
- this.
- See Also:
-
replaceWith
$replaceWith
replaces 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
$replaceWith
replaces the input document with the value.- Parameters:
value
- must not be null.- Returns:
- this.
-
set
Fluent API variant for$set
adding a singlepipeline operation
every time. To update multiple fields within oneSetOperation
useset(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:UpdateDefinition
If true prevents a write operation that affects multiple documents from yielding to other reads or writes once the first document is written.- Specified by:
isIsolated
in interfaceUpdateDefinition
- Returns:
- true if update isolated is set.
-
getUpdateObject
public org.bson.Document getUpdateObject()- Specified by:
getUpdateObject
in interfaceUpdateDefinition
- Returns:
- the actual update in its native
Document
format. Never null.
-
modifies
Description copied from interface:UpdateDefinition
Check if a given key is modified by applying the update.- Specified by:
modifies
in interfaceUpdateDefinition
- Parameters:
key
- must not be null.- Returns:
- true if the actual
UpdateDefinition
attempts to modify the given key.
-
inc
Description copied from interface:UpdateDefinition
Increment the value of a given key by1
.- Specified by:
inc
in interfaceUpdateDefinition
- Parameters:
key
- must not be null.
-
getArrayFilters
Description copied from interface:UpdateDefinition
Get the specification which elements to modify in an array field.UpdateDefinition.ArrayFilter
are passed directly to the driver without further type or field mapping.- Specified by:
getArrayFilters
in interfaceUpdateDefinition
- Returns:
- never null.
-
toString
- Overrides:
toString
in classAggregation
-