Class UpdateOperators

java.lang.Object
dev.morphia.query.updates.UpdateOperators

public final class UpdateOperators extends Object
Defines helper methods for specifying operations for updates or findAndModify
Since:
2.0
See Also:
  • Method Details

    • addToSet

      public static UpdateOperator addToSet(String field, Object value)
      The $addToSet operator adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array.
      Parameters:
      field - the field to updated
      value - the value to add
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $addToSet
    • addToSet

      public static UpdateOperator addToSet(String field, List<?> values)
      The $addToSet operator adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array. By default, this operator will use $each to add each element in the given list to the stored array in the document.
      Parameters:
      field - the field to updated
      values - the values to add
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $addToSet
      Update Operator: $each
    • and

      public static UpdateOperator and(String field, int value)
      The $bit operator performs a bitwise update of a field. The operator supports bitwise and, bitwise or, and bitwise xor (i.e. exclusive or) operations.
      Parameters:
      field - the field to update
      value - the value to use for the update
      Returns:
      the update operator
      See Also:
      MongoDB documentation
      Update Operator: $bit
    • bit

      public static UpdateOperator bit(String field, Number value)
      Multiplies the value of the field by the specified amount.
      Parameters:
      field - the field to multiply
      value - the number to multiply by
      Returns:
      the update operator
      Since:
      3.0
      Since server release
      5.0
      MongoDB documentation
      Update Operator: $bit
    • currentDate

      public static CurrentDateOperator currentDate(String field)
      The $currentDate operator sets the value of a field to the current date, either as a Date or a timestamp. The default type is Date.
      Parameters:
      field - the field to set
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $currentDate
    • dec

      public static UpdateOperator dec(String field)
      Decrements the value of the field by one.
      Parameters:
      field - the field to decrement
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $dec
    • dec

      public static UpdateOperator dec(String field, Number value)
      Decrements the value of the field by the specified amount.
      Parameters:
      field - the field to decrement
      value - the number to decrement by
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $dec
    • inc

      public static UpdateOperator inc(String field, Number value)
      Increments the value of the field by the specified amount.
      Parameters:
      field - the field to increment
      value - the number to increment by
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $inc
    • inc

      public static UpdateOperator inc(String field)
      Increments the value of the field by one.
      Parameters:
      field - the field to increment
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $inc
    • max

      public static UpdateOperator max(String field, Number value)
      Only updates the field if the specified value is greater than the existing field value.
      Parameters:
      field - the field to update
      value - the max value to apply
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $max
    • max

      public static UpdateOperator max(String field, Temporal value)
      Only updates the field if the specified value is greater than the existing field value.
      Parameters:
      field - the field to update
      value - the max value to apply
      Returns:
      the update operator
      Since:
      2.2
      MongoDB documentation
      Update Operator: $max
    • max

      public static UpdateOperator max(String field, Date value)
      Only updates the field if the specified value is greater than the existing field value.
      Parameters:
      field - the field to update
      value - the max value to apply
      Returns:
      the update operator
      Since:
      2.2
      MongoDB documentation
      Update Operator: $max
    • min

      public static UpdateOperator min(String field, Number value)
      Only updates the field if the specified value is less than the existing field value.
      Parameters:
      field - the field to update
      value - the min value to apply
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $min
    • min

      public static UpdateOperator min(String field, Temporal value)
      Only updates the field if the specified value is less than the existing field value.
      Parameters:
      field - the field to update
      value - the min value to apply
      Returns:
      the update operator
      Since:
      2.2
      MongoDB documentation
      Update Operator: $min
    • min

      public static UpdateOperator min(String field, Date value)
      Only updates the field if the specified value is less than the existing field value.
      Parameters:
      field - the field to update
      value - the min value to apply
      Returns:
      the update operator
      Since:
      2.2
      MongoDB documentation
      Update Operator: $min
    • mul

      public static UpdateOperator mul(String field, Number value)
      Multiplies the value of the field by the specified amount.
      Parameters:
      field - the field to multiply
      value - the number to multiply by
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $mul
    • or

      public static UpdateOperator or(String field, int value)
      The $bit operator performs a bitwise update of a field. The operator supports bitwise and, bitwise or, and bitwise xor (i.e. exclusive or) operations.
      Parameters:
      field - the field to update
      value - the value to use for the update
      Returns:
      the update operator
      See Also:
      MongoDB documentation
      Update Operator: $bit
    • pop

      public static PopOperator pop(String field)
      The $pop operator removes the first or last element of an array. This operation can remove the first of the last value but defaults to removing the last.
      Parameters:
      field - the field to update
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $pop
    • pull

      public static UpdateOperator pull(String field, Filter... filters)
      The $pull operator removes from an existing array all instances of a value or values that match a specified condition.
      Parameters:
      field - the field to update
      filters - the filters to apply
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $pull
    • pull

      public static UpdateOperator pull(String field, Object value)
      The $pull operator removes from an existing array all instances of a value or values that match a specified condition.
      Parameters:
      field - the field to update
      value - the value to pull
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $pull
    • pullAll

      public static UpdateOperator pullAll(String field, List<?> values)
      The $pullAll operator removes all instances of the specified values from an existing array. Unlike the $pull operator that removes elements by specifying a query, $pullAll removes elements that match the listed values.
      Parameters:
      field - the field to update
      values - the values to remove
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $pullAll
    • push

      public static PushOperator push(String field, Object value)
      The $push operator appends a specified value to an array.
      Parameters:
      field - the field to update
      value - the value to add
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $push
    • push

      public static PushOperator push(String field, List<?> values)
      The $push operator appends a specified value to an array.
      Parameters:
      field - the field to update
      values - the values to add
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $each
      Update Operator: $push
    • rename

      public static UpdateOperator rename(String field, String value)
      Renames a field.
      Parameters:
      field - the field to rename
      value - the new name
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $rename
    • set

      public static UpdateOperator set(String field, Object value)
      The $set operator replaces the value of a field with the specified value.
      Parameters:
      field - the field to set
      value - the value to set
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $set
    • set

      public static UpdateOperator set(Object value)
      The $set operator replaces the value of a field with the specified value.
      Parameters:
      value - the value to set
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $set
    • setOnInsert

      public static UpdateOperator setOnInsert(Map<String,Object> values)
      If an update operation with upsert: true results in an insert of a document, then $setOnInsert assigns the specified values to the fields in the document. If the update operation does not result in an insert, $setOnInsert does nothing.
      Parameters:
      values - the fields and their values to insert
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $setOnInsert
    • unset

      public static UpdateOperator unset(String field, String... others)
      The $unset operator deletes a particular field.
      Parameters:
      field - the field to set
      Returns:
      the update operator
      MongoDB documentation
      Update Operator: $unset
    • xor

      public static UpdateOperator xor(String field, int value)
      The $bit operator performs a bitwise update of a field. The operator supports bitwise and, bitwise or, and bitwise xor (i.e. exclusive or) operations.
      Parameters:
      field - the field to update
      value - the value to use for the update
      Returns:
      the update operator
      See Also:
      MongoDB documentation
      Update Operator: $bit