public class AggregationUpdate extends Aggregation implements UpdateDefinition
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 }
)
| Modifier and Type | Class and Description |
|---|---|
static interface |
AggregationUpdate.SetValueAppender
Fluent API AggregationUpdate builder.
|
UpdateDefinition.ArrayFilterCURRENT, DEFAULT_CONTEXT, DEFAULT_OPTIONS, operations, REMOVE, ROOT| Modifier | Constructor and Description |
|---|---|
protected |
AggregationUpdate()
Create new
AggregationUpdate. |
protected |
AggregationUpdate(List<AggregationOperation> pipeline)
Create new
AggregationUpdate with the given aggregation pipeline to apply. |
| Modifier and Type | Method and Description |
|---|---|
static AggregationUpdate |
from(List<AggregationOperation> pipeline)
Create a new AggregationUpdate from the given
AggregationOperations. |
List<UpdateDefinition.ArrayFilter> |
getArrayFilters()
Get the specification which elements to modify in an array field.
|
org.bson.Document |
getUpdateObject() |
void |
inc(String key)
Increment the value of a given key by
1. |
Boolean |
isIsolated()
If true prevents a write operation that affects multiple documents from yielding to
other reads or writes once the first document is written.
|
AggregationUpdate |
isolated()
Prevents a write operation that affects multiple documents from yielding to other reads or writes
once the first document is written.
|
boolean |
modifies(String key)
Check if a given key is modified by applying the update.
|
AggregationUpdate |
replaceWith(Object value)
$replaceWith replaces the input document with the value. |
AggregationUpdate |
replaceWith(ReplaceWithOperation replaceWithOperation)
$replaceWith replaces the input document with the specified document. |
AggregationUpdate |
set(SetOperation setOperation)
Adds new fields to documents.
|
AggregationUpdate.SetValueAppender |
set(String key)
Fluent API variant for
$set adding a single pipeline operation every time. |
String |
toString() |
AggregationUpdate |
unset(String... keys)
Short for
unset(UnsetOperation). |
AggregationUpdate |
unset(UnsetOperation unsetOperation)
$unset removes/excludes fields from documents. |
static AggregationUpdate |
update()
Start defining the update pipeline to execute.
|
asAggregationList, bind, bucket, bucket, bucketAuto, bucketAuto, count, facet, facet, fields, geoNear, getOptions, graphLookup, group, group, limit, lookup, lookup, match, match, newAggregation, newAggregation, newAggregation, newAggregation, newAggregationOptions, newUpdate, out, previousOperation, project, project, project, replaceRoot, replaceRoot, replaceRoot, sample, skip, skip, sort, sort, sortByCount, sortByCount, toDocument, toPipeline, unwind, unwind, unwind, unwind, withOptionsclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waithasArrayFiltersprotected AggregationUpdate()
AggregationUpdate.protected AggregationUpdate(List<AggregationOperation> pipeline)
AggregationUpdate with the given aggregation pipeline to apply.pipeline - must not be null.public static AggregationUpdate update()
AggregationUpdate.public static AggregationUpdate from(List<AggregationOperation> pipeline)
AggregationOperations.AggregationUpdate.public AggregationUpdate set(SetOperation setOperation)
$set outputs documents that contain all existing fields from the input
documents and newly added fields.setOperation - must not be null.public AggregationUpdate unset(UnsetOperation unsetOperation)
$unset removes/excludes fields from documents.unsetOperation - must not be null.public AggregationUpdate replaceWith(ReplaceWithOperation replaceWithOperation)
$replaceWith replaces the input document with the specified document. The operation replaces all existing
fields in the input document, including the _id field.replaceWithOperation - public AggregationUpdate replaceWith(Object value)
$replaceWith replaces the input document with the value.value - must not be null.public AggregationUpdate.SetValueAppender set(String key)
$set adding a single pipeline operation every time. To update
multiple fields within one SetOperation use set(SetOperation).key - must not be null.AggregationUpdate.SetValueAppender.set(SetOperation)public AggregationUpdate unset(String... keys)
unset(UnsetOperation).keys - public AggregationUpdate isolated()
MongoOperations.updateMulti(Query, UpdateDefinition, Class).public Boolean isIsolated()
UpdateDefinitionisIsolated in interface UpdateDefinitionpublic org.bson.Document getUpdateObject()
getUpdateObject in interface UpdateDefinitionDocument format. Never null.public boolean modifies(String key)
UpdateDefinitionmodifies in interface UpdateDefinitionkey - must not be null.UpdateDefinition attempts to modify the given key.public void inc(String key)
UpdateDefinition1.inc in interface UpdateDefinitionkey - must not be null.public List<UpdateDefinition.ArrayFilter> getArrayFilters()
UpdateDefinitionUpdateDefinition.ArrayFilter are passed directly to the
driver without further type or field mapping.getArrayFilters in interface UpdateDefinitionpublic String toString()
toString in class AggregationCopyright © 2011–2020 Pivotal Software, Inc.. All rights reserved.