java.lang.Object
org.springframework.data.mongodb.core.query.Update
All Implemented Interfaces:
UpdateDefinition
Direct Known Subclasses:
BasicUpdate

public class Update extends Object implements UpdateDefinition
Class to easily construct MongoDB update clauses.
Author:
Thomas Risberg, Mark Pollack, Oliver Gierke, Becca Gaspard, Christoph Strobl, Thomas Darimont, Alexey Plotnik, Mark Paluch, Pavel Vodrazka
  • Constructor Details

    • Update

      public Update()
  • Method Details

    • update

      public static Update update(String key, @Nullable Object value)
      Static factory method to create an Update using the provided key
      Parameters:
      key - the field to update.
      Returns:
      new instance of Update.
    • fromDocument

      public static Update fromDocument(org.bson.Document object, String... exclude)
      Creates an Update instance from the given Document. Allows to explicitly exclude fields from making it into the created Update object. Note, that this will set attributes directly and not use $set. This means fields not given in the Document will be nulled when executing the update. To create an only-updating Update instance of a Document, call set(String, Object) for each value in it.
      Parameters:
      object - the source Document to create the update from.
      exclude - the fields to exclude.
      Returns:
      new instance of Update.
    • set

      public Update set(String key, @Nullable Object value)
      Update using the $set update modifier
      Parameters:
      key - the field name.
      value - can be null. In this case the property remains in the db with a null value. To remove it use unset(String).
      Returns:
      this.
      See Also:
    • setOnInsert

      public Update setOnInsert(String key, @Nullable Object value)
      Update using the $setOnInsert update modifier
      Parameters:
      key - the field name.
      value - can be null.
      Returns:
      this.
      See Also:
    • unset

      public Update unset(String key)
      Update using the $unset update modifier
      Parameters:
      key - the field name.
      Returns:
      this.
      See Also:
    • inc

      public Update inc(String key, Number inc)
      Update using the $inc update modifier
      Parameters:
      key - the field name.
      inc - must not be null.
      Returns:
      this.
      See Also:
    • inc

      public void inc(String key)
      Description copied from interface: UpdateDefinition
      Increment the value of a given key by 1.
      Specified by:
      inc in interface UpdateDefinition
      Parameters:
      key - must not be null.
    • push

      public Update push(String key, @Nullable Object value)
      Update using the $push update modifier
      Parameters:
      key - the field name.
      value - can be null.
      Returns:
      this.
      See Also:
    • push

      Update using $push modifier.
      Allows creation of $push command for single or multiple (using $each) values as well as using $position.
      Parameters:
      key - the field name.
      Returns:
      Update.PushOperatorBuilder for given key
      See Also:
    • addToSet

      public Update.AddToSetBuilder addToSet(String key)
      Update using $addToSet modifier.
      Allows creation of $push command for single or multiple (using $each) values
      Parameters:
      key - the field name.
      Returns:
      new instance of Update.AddToSetBuilder.
      Since:
      1.5
    • addToSet

      public Update addToSet(String key, @Nullable Object value)
      Update using the $addToSet update modifier
      Parameters:
      key - the field name.
      value - can be null.
      Returns:
      this.
      See Also:
    • pop

      public Update pop(String key, Update.Position pos)
      Update using the $pop update modifier
      Parameters:
      key - the field name.
      pos - must not be null.
      Returns:
      this.
      See Also:
    • pull

      public Update pull(String key, @Nullable Object value)
      Update using the $pull update modifier
      Parameters:
      key - the field name.
      value - can be null.
      Returns:
      this.
      See Also:
    • pullAll

      public Update pullAll(String key, Object[] values)
      Update using the $pullAll update modifier
      Parameters:
      key - the field name.
      values - must not be null.
      Returns:
      this.
      See Also:
    • rename

      public Update rename(String oldName, String newName)
      Update using the $rename update modifier
      Parameters:
      oldName - must not be null.
      newName - must not be null.
      Returns:
      this.
      See Also:
    • currentDate

      public Update currentDate(String key)
      Update given key to current date using $currentDate modifier.
      Parameters:
      key - the field name.
      Returns:
      this.
      Since:
      1.6
      See Also:
    • currentTimestamp

      public Update currentTimestamp(String key)
      Update given key to current date using $currentDate : { $type : "timestamp" } modifier.
      Parameters:
      key - the field name.
      Returns:
      this.
      Since:
      1.6
      See Also:
    • multiply

      public Update multiply(String key, Number multiplier)
      Multiply the value of given key by the given number.
      Parameters:
      key - must not be null.
      multiplier - must not be null.
      Returns:
      this.
      Since:
      1.7
      See Also:
    • max

      public Update max(String key, Object value)
      Update given key to the value if the value is greater than the current value of the field.
      Parameters:
      key - must not be null.
      value - must not be null.
      Returns:
      this.
      Since:
      1.10
      See Also:
    • min

      public Update min(String key, Object value)
      Update given key to the value if the value is less than the current value of the field.
      Parameters:
      key - must not be null.
      value - must not be null.
      Returns:
      this.
      Since:
      1.10
      See Also:
    • bitwise

      public Update.BitwiseOperatorBuilder bitwise(String key)
      The operator supports bitwise and, bitwise or, and bitwise xor operations.
      Parameters:
      key - the field name.
      Returns:
      this.
      Since:
      1.7
    • isolated

      public Update isolated()
      Prevents a write operation that affects multiple documents from yielding to other reads or writes once the first document is written.
      Use with MongoOperations.updateMulti(Query, UpdateDefinition, Class).
      Returns:
      this.
      Since:
      2.0
    • filterArray

      public Update filterArray(CriteriaDefinition criteria)
      Filter elements in an array that match the given criteria for update. CriteriaDefinition is passed directly to the driver without further type or field mapping.
      Parameters:
      criteria - must not be null.
      Returns:
      this.
      Since:
      2.2
    • filterArray

      public Update filterArray(String identifier, Object expression)
      Filter elements in an array that match the given criteria for update. expression is used directly with the driver without further further type or field mapping.
      Parameters:
      identifier - the positional operator identifier filter criteria name.
      expression - the positional operator filter expression.
      Returns:
      this.
      Since:
      2.2
    • isIsolated

      public Boolean 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 interface UpdateDefinition
      Returns:
      true if update isolated is set.
    • getUpdateObject

      public org.bson.Document getUpdateObject()
      Specified by:
      getUpdateObject in interface UpdateDefinition
      Returns:
      the actual update in its native Document format. Never null.
    • getArrayFilters

      public List<UpdateDefinition.ArrayFilter> 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 interface UpdateDefinition
      Returns:
      never null.
    • hasArrayFilters

      public boolean hasArrayFilters()
      Specified by:
      hasArrayFilters in interface UpdateDefinition
      Returns:
      true if UpdateDefinition contains array filters.
    • addMultiFieldOperation

      protected void addMultiFieldOperation(String operator, String key, @Nullable Object value)
    • modifies

      public boolean modifies(String key)
      Determine if a given key will be touched on execution.
      Specified by:
      modifies in interface UpdateDefinition
      Parameters:
      key - the field name.
      Returns:
      true if given field is updated.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(@Nullable Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object