Class Updates


  • public final class Updates
    extends Object
    A factory for document updates. A convenient way to use this class is to statically import all of its methods, which allows usage like:
        collection.updateOne(eq("x", 1), set("x", 2));
     
    Since:
    3.1
    MongoDB documentation
    Update Operators
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <TItem> org.bson.conversions.Bson addEachToSet​(String fieldName, List<TItem> values)
      Creates an update that adds each of the given values to the array value of the field with the given name, unless the value is already present, in which case it does nothing
      static <TItem> org.bson.conversions.Bson addToSet​(String fieldName, TItem value)
      Creates an update that adds the given value to the array value of the field with the given name, unless the value is already present, in which case it does nothing
      static org.bson.conversions.Bson bitwiseAnd​(String fieldName, int value)
      Creates an update that performs a bitwise and between the given integer value and the integral value of the field with the given name.
      static org.bson.conversions.Bson bitwiseAnd​(String fieldName, long value)
      Creates an update that performs a bitwise and between the given long value and the integral value of the field with the given name.
      static org.bson.conversions.Bson bitwiseOr​(String fieldName, int value)
      Creates an update that performs a bitwise or between the given integer value and the integral value of the field with the given name.
      static org.bson.conversions.Bson bitwiseOr​(String fieldName, long value)
      Creates an update that performs a bitwise or between the given long value and the integral value of the field with the given name.
      static org.bson.conversions.Bson bitwiseXor​(String fieldName, int value)
      Creates an update that performs a bitwise xor between the given integer value and the integral value of the field with the given name.
      static org.bson.conversions.Bson bitwiseXor​(String fieldName, long value)
      Creates an update that performs a bitwise xor between the given long value and the integral value of the field with the given name.
      static org.bson.conversions.Bson combine​(List<? extends org.bson.conversions.Bson> updates)
      Combine a list of updates into a single update.
      static org.bson.conversions.Bson combine​(org.bson.conversions.Bson... updates)
      Combine a list of updates into a single update.
      static org.bson.conversions.Bson currentDate​(String fieldName)
      Creates an update that sets the value of the field to the current date as a BSON date.
      static org.bson.conversions.Bson currentTimestamp​(String fieldName)
      Creates an update that sets the value of the field to the current date as a BSON timestamp.
      static org.bson.conversions.Bson inc​(String fieldName, Number number)
      Creates an update that increments the value of the field with the given name by the given value.
      static <TItem> org.bson.conversions.Bson max​(String fieldName, TItem value)
      Creates an update that sets the value of the field to the given value if the given value is greater than the current value of the field.
      static <TItem> org.bson.conversions.Bson min​(String fieldName, TItem value)
      Creates an update that sets the value of the field to the given value if the given value is less than the current value of the field.
      static org.bson.conversions.Bson mul​(String fieldName, Number number)
      Creates an update that multiplies the value of the field with the given name by the given number.
      static org.bson.conversions.Bson popFirst​(String fieldName)
      Creates an update that pops the first element of an array that is the value of the field with the given name.
      static org.bson.conversions.Bson popLast​(String fieldName)
      Creates an update that pops the last element of an array that is the value of the field with the given name.
      static <TItem> org.bson.conversions.Bson pull​(String fieldName, TItem value)
      Creates an update that removes all instances of the given value from the array value of the field with the given name.
      static <TItem> org.bson.conversions.Bson pullAll​(String fieldName, List<TItem> values)
      Creates an update that removes all instances of the given values from the array value of the field with the given name.
      static org.bson.conversions.Bson pullByFilter​(org.bson.conversions.Bson filter)
      Creates an update that removes from an array all elements that match the given filter.
      static <TItem> org.bson.conversions.Bson push​(String fieldName, TItem value)
      Creates an update that adds the given value to the array value of the field with the given name.
      static <TItem> org.bson.conversions.Bson pushEach​(String fieldName, List<TItem> values)
      Creates an update that adds each of the given values to the array value of the field with the given name.
      static <TItem> org.bson.conversions.Bson pushEach​(String fieldName, List<TItem> values, PushOptions options)
      Creates an update that adds each of the given values to the array value of the field with the given name, applying the given options for positioning the pushed values, and then slicing and/or sorting the array.
      static org.bson.conversions.Bson rename​(String fieldName, String newFieldName)
      Creates an update that renames a field.
      static <TItem> org.bson.conversions.Bson set​(String fieldName, TItem value)
      Creates an update that sets the value of the field with the given name to the given value.
      static <TItem> org.bson.conversions.Bson setOnInsert​(String fieldName, TItem value)
      Creates an update that sets the value of the field with the given name to the given value, but only if the update is an upsert that results in an insert of a document.
      static org.bson.conversions.Bson setOnInsert​(org.bson.conversions.Bson value)
      Creates an update that sets the values for the document, but only if the update is an upsert that results in an insert of a document.
      static org.bson.conversions.Bson unset​(String fieldName)
      Creates an update that deletes the field with the given name.
    • Method Detail

      • combine

        public static org.bson.conversions.Bson combine​(org.bson.conversions.Bson... updates)
        Combine a list of updates into a single update.
        Parameters:
        updates - the list of updates
        Returns:
        a combined update
      • combine

        public static org.bson.conversions.Bson combine​(List<? extends org.bson.conversions.Bson> updates)
        Combine a list of updates into a single update.
        Parameters:
        updates - the list of updates
        Returns:
        a combined update
      • set

        public static <TItem> org.bson.conversions.Bson set​(String fieldName,
                                                            @Nullable
                                                            TItem value)
        Creates an update that sets the value of the field with the given name to the given value.
        Type Parameters:
        TItem - the value type
        Parameters:
        fieldName - the non-null field name
        value - the value, which may be null
        Returns:
        the update
        MongoDB documentation
        $set
      • unset

        public static org.bson.conversions.Bson unset​(String fieldName)
        Creates an update that deletes the field with the given name.
        Parameters:
        fieldName - the non-null field name
        Returns:
        the update
        MongoDB documentation
        $unset
      • setOnInsert

        public static org.bson.conversions.Bson setOnInsert​(org.bson.conversions.Bson value)
        Creates an update that sets the values for the document, but only if the update is an upsert that results in an insert of a document.
        Parameters:
        value - the value
        Returns:
        the update
        Since:
        3.10.0
        See Also:
        UpdateOptions.upsert(boolean)
        MongoDB documentation
        $setOnInsert
      • setOnInsert

        public static <TItem> org.bson.conversions.Bson setOnInsert​(String fieldName,
                                                                    @Nullable
                                                                    TItem value)
        Creates an update that sets the value of the field with the given name to the given value, but only if the update is an upsert that results in an insert of a document.
        Type Parameters:
        TItem - the value type
        Parameters:
        fieldName - the non-null field name
        value - the value, which may be null
        Returns:
        the update
        See Also:
        UpdateOptions.upsert(boolean)
        MongoDB documentation
        $setOnInsert
      • rename

        public static org.bson.conversions.Bson rename​(String fieldName,
                                                       String newFieldName)
        Creates an update that renames a field.
        Parameters:
        fieldName - the non-null field name
        newFieldName - the non-null new field name
        Returns:
        the update
        MongoDB documentation
        $rename
      • inc

        public static org.bson.conversions.Bson inc​(String fieldName,
                                                    Number number)
        Creates an update that increments the value of the field with the given name by the given value.
        Parameters:
        fieldName - the non-null field name
        number - the value
        Returns:
        the update
        MongoDB documentation
        $inc
      • mul

        public static org.bson.conversions.Bson mul​(String fieldName,
                                                    Number number)
        Creates an update that multiplies the value of the field with the given name by the given number.
        Parameters:
        fieldName - the non-null field name
        number - the non-null number
        Returns:
        the update
        MongoDB documentation
        $mul
      • min

        public static <TItem> org.bson.conversions.Bson min​(String fieldName,
                                                            TItem value)
        Creates an update that sets the value of the field to the given value if the given value is less than the current value of the field.
        Type Parameters:
        TItem - the value type
        Parameters:
        fieldName - the non-null field name
        value - the value
        Returns:
        the update
        MongoDB documentation
        $min
      • max

        public static <TItem> org.bson.conversions.Bson max​(String fieldName,
                                                            TItem value)
        Creates an update that sets the value of the field to the given value if the given value is greater than the current value of the field.
        Type Parameters:
        TItem - the value type
        Parameters:
        fieldName - the non-null field name
        value - the value
        Returns:
        the update
        MongoDB documentation
        $min
      • currentDate

        public static org.bson.conversions.Bson currentDate​(String fieldName)
        Creates an update that sets the value of the field to the current date as a BSON date.
        Parameters:
        fieldName - the non-null field name
        Returns:
        the update
        MongoDB documentation
        $currentDate
        Date
      • currentTimestamp

        public static org.bson.conversions.Bson currentTimestamp​(String fieldName)
        Creates an update that sets the value of the field to the current date as a BSON timestamp.
        Parameters:
        fieldName - the non-null field name
        Returns:
        the update
        MongoDB documentation
        $currentDate
        Timestamp
      • addToSet

        public static <TItem> org.bson.conversions.Bson addToSet​(String fieldName,
                                                                 @Nullable
                                                                 TItem value)
        Creates an update that adds the given value to the array value of the field with the given name, unless the value is already present, in which case it does nothing
        Type Parameters:
        TItem - the value type
        Parameters:
        fieldName - the non-null field name
        value - the value, which may be null
        Returns:
        the update
        MongoDB documentation
        $addToSet
      • addEachToSet

        public static <TItem> org.bson.conversions.Bson addEachToSet​(String fieldName,
                                                                     List<TItem> values)
        Creates an update that adds each of the given values to the array value of the field with the given name, unless the value is already present, in which case it does nothing
        Type Parameters:
        TItem - the value type
        Parameters:
        fieldName - the non-null field name
        values - the values
        Returns:
        the update
        MongoDB documentation
        $addToSet
      • push

        public static <TItem> org.bson.conversions.Bson push​(String fieldName,
                                                             @Nullable
                                                             TItem value)
        Creates an update that adds the given value to the array value of the field with the given name.
        Type Parameters:
        TItem - the value type
        Parameters:
        fieldName - the non-null field name
        value - the value, which may be null
        Returns:
        the update
        MongoDB documentation
        $push
      • pushEach

        public static <TItem> org.bson.conversions.Bson pushEach​(String fieldName,
                                                                 List<TItem> values)
        Creates an update that adds each of the given values to the array value of the field with the given name.
        Type Parameters:
        TItem - the value type
        Parameters:
        fieldName - the non-null field name
        values - the values
        Returns:
        the update
        MongoDB documentation
        $push
      • pushEach

        public static <TItem> org.bson.conversions.Bson pushEach​(String fieldName,
                                                                 List<TItem> values,
                                                                 PushOptions options)
        Creates an update that adds each of the given values to the array value of the field with the given name, applying the given options for positioning the pushed values, and then slicing and/or sorting the array.
        Type Parameters:
        TItem - the value type
        Parameters:
        fieldName - the non-null field name
        values - the values
        options - the non-null push options
        Returns:
        the update
        MongoDB documentation
        $push
      • pull

        public static <TItem> org.bson.conversions.Bson pull​(String fieldName,
                                                             @Nullable
                                                             TItem value)
        Creates an update that removes all instances of the given value from the array value of the field with the given name.
        Type Parameters:
        TItem - the value type
        Parameters:
        fieldName - the non-null field name
        value - the value, which may be null
        Returns:
        the update
        MongoDB documentation
        $pull
      • pullByFilter

        public static org.bson.conversions.Bson pullByFilter​(org.bson.conversions.Bson filter)
        Creates an update that removes from an array all elements that match the given filter.
        Parameters:
        filter - the query filter
        Returns:
        the update
        MongoDB documentation
        $pull
      • pullAll

        public static <TItem> org.bson.conversions.Bson pullAll​(String fieldName,
                                                                List<TItem> values)
        Creates an update that removes all instances of the given values from the array value of the field with the given name.
        Type Parameters:
        TItem - the value type
        Parameters:
        fieldName - the non-null field name
        values - the values
        Returns:
        the update
        MongoDB documentation
        $pull
      • popFirst

        public static org.bson.conversions.Bson popFirst​(String fieldName)
        Creates an update that pops the first element of an array that is the value of the field with the given name.
        Parameters:
        fieldName - the non-null field name
        Returns:
        the update
        MongoDB documentation
        $pop
      • popLast

        public static org.bson.conversions.Bson popLast​(String fieldName)
        Creates an update that pops the last element of an array that is the value of the field with the given name.
        Parameters:
        fieldName - the non-null field name
        Returns:
        the update
        MongoDB documentation
        $pop
      • bitwiseAnd

        public static org.bson.conversions.Bson bitwiseAnd​(String fieldName,
                                                           int value)
        Creates an update that performs a bitwise and between the given integer value and the integral value of the field with the given name.
        Parameters:
        fieldName - the field name
        value - the value
        Returns:
        the update
      • bitwiseAnd

        public static org.bson.conversions.Bson bitwiseAnd​(String fieldName,
                                                           long value)
        Creates an update that performs a bitwise and between the given long value and the integral value of the field with the given name.
        Parameters:
        fieldName - the field name
        value - the value
        Returns:
        the update
        MongoDB documentation
        $bit
      • bitwiseOr

        public static org.bson.conversions.Bson bitwiseOr​(String fieldName,
                                                          int value)
        Creates an update that performs a bitwise or between the given integer value and the integral value of the field with the given name.
        Parameters:
        fieldName - the field name
        value - the value
        Returns:
        the update
        MongoDB documentation
        $bit
      • bitwiseOr

        public static org.bson.conversions.Bson bitwiseOr​(String fieldName,
                                                          long value)
        Creates an update that performs a bitwise or between the given long value and the integral value of the field with the given name.
        Parameters:
        fieldName - the field name
        value - the value
        Returns:
        the update
        MongoDB documentation
        $bit
      • bitwiseXor

        public static org.bson.conversions.Bson bitwiseXor​(String fieldName,
                                                           int value)
        Creates an update that performs a bitwise xor between the given integer value and the integral value of the field with the given name.
        Parameters:
        fieldName - the field name
        value - the value
        Returns:
        the update
      • bitwiseXor

        public static org.bson.conversions.Bson bitwiseXor​(String fieldName,
                                                           long value)
        Creates an update that performs a bitwise xor between the given long value and the integral value of the field with the given name.
        Parameters:
        fieldName - the field name
        value - the value
        Returns:
        the update