Class MutateInSpec

java.lang.Object
com.couchbase.client.java.kv.MutateInSpec
Direct Known Subclasses:
ArrayAddUnique, ArrayAppend, ArrayInsert, ArrayPrepend, Increment, Insert, Remove, Replace, ReplaceBodyWithXattr, Upsert

public abstract class MutateInSpec extends Object
Defines specs to mutate parts in a JSON document.

Note that each spec only specifies a portion of a document. Whether the whole document is to be upserted or inserted is controlled by MutateInOptions.storeSemantics(StoreSemantics) on the outer options block.

Some operations allow to specify an empty path ("") which means that the root document level is used (so it will be applied to the full document). By nature it makes sense to only use such a command in isolation, but it can be combined with xattr (extended attributes, a document metadata section) operations as well.

  • Constructor Details

    • MutateInSpec

      public MutateInSpec()
  • Method Details

    • toCore

      @Internal public abstract CoreSubdocMutateCommand toCore(JsonSerializer serializer)
      Internal operation called from the encoding side that encodes the spec into its internal representation.
      Parameters:
      serializer - the serializer that should be used.
      Returns:
      the encoded command.
    • replace

      public static Replace replace(String path, Object value)
      Creates a spec with the intention of replacing an existing value in a JSON document.

      If the path is empty (""), then the value will be used for the document's full body. Will error if the last element of the path does not exist.

      Parameters:
      path - the path identifying where to replace the value.
      value - the value to replace with.
      Returns:
      the created MutateInSpec.
    • insert

      public static Insert insert(String path, Object value)
      Creates a command with the intention of inserting a new value in a JSON object.

      Will error if the last element of the path already exists.

      Parameters:
      path - the path identifying where to insert the value.
      value - the value to insert
      Returns:
      the created MutateInSpec.
    • remove

      public static Remove remove(String path)
      Creates a command with the intention of removing an existing value in a JSON object.

      Will error if the path does not exist.

      Parameters:
      path - the path identifying what to remove.
      Returns:
      the created MutateInSpec.
    • upsert

      public static Upsert upsert(String path, Object value)
      Creates a command with the intention of upserting a value in a JSON object.

      That is, the value will be replaced if the path already exists, or inserted if not.

      Parameters:
      path - the path identifying where to upsert the value.
      value - the value to upsert.
      Returns:
      the created MutateInSpec.
    • arrayAppend

      public static ArrayAppend arrayAppend(String path, List<?> values)
      Creates a command with the intention of appending a value to an existing JSON array.

      Will error if the last element of the path does not exist or is not an array.

      Parameters:
      path - the path identifying an array to which to append the value.
      values - the value(s) to append.
      Returns:
      the created MutateInSpec.
    • arrayPrepend

      public static ArrayPrepend arrayPrepend(String path, List<?> values)
      Creates a command with the intention of prepending a value to an existing JSON array.

      Will error if the last element of the path does not exist or is not an array.

      Parameters:
      path - the path identifying an array to which to append the value.
      values - the value(s) to prepend.
      Returns:
      the created MutateInSpec.
    • arrayInsert

      public static ArrayInsert arrayInsert(String path, List<?> values)
      Creates a command with the intention of inserting a value into an existing JSON array.

      Will error if the last element of the path does not exist or is not an array.

      Parameters:
      path - the path identifying an array to which to append the value, and an index. E.g. "foo.bar[3]"
      values - the value(s) to insert.
      Returns:
      the created MutateInSpec.
    • arrayAddUnique

      public static ArrayAddUnique arrayAddUnique(String path, Object value)
      Creates a command with the intent of inserting a value into an existing JSON array, but only if the value is not already contained in the array (by way of string comparison).

      Will error if the last element of the path does not exist or is not an array.

      Parameters:
      path - the path identifying an array to which to append the value. For example: "foo.bar"
      value - the value to insert.
      Returns:
      the created MutateInSpec.
    • increment

      public static Increment increment(String path, long delta)
      Creates a command with the intent of incrementing a numerical field in a JSON object. If the field does not exist then it is created and takes the value of `delta`.
      Parameters:
      path - the path identifying a numerical field to adjust or create.
      delta - the value to increment the field by.
      Returns:
      the created MutateInSpec.
    • decrement

      public static Increment decrement(String path, long delta)
      Creates a command with the intent of decrementing a numerical field in a JSON object. If the field does not exist then it is created and takes the value of `delta` * -1.
      Parameters:
      path - the path identifying a numerical field to adjust or create.
      delta - the value to increment the field by.
      Returns:
      the created MutateInSpec.