class MutateInBuilder (View source)

A builder for subdocument mutations. In order to perform the final set of operations, use the execute() method.

Instances of this builder should be obtained through \Couchbase\Bucket->mutateIn()

Constants

FULLDOC_REPLACE

FULLDOC_UPSERT

FULLDOC_INSERT

Methods

insert(string $path, mixed $value, array|bool $options = [])

Insert a fragment provided the last element of the path doesn't exists.

modeDocument(int $mode)

Select mode for new full-document operations.

upsert(string $path, mixed $value, array|bool $options = [])

Insert a fragment, replacing the old value if the path exists.

replace(string $path, mixed $value, array $options = [])

Replace an existing value by the given fragment

remove(string $path, array $options = [])

Remove an entry in a JSON document.

arrayPrepend(string $path, mixed $value, array|bool $options = [])

Prepend to an existing array, pushing the value to the front/first position in the array.

arrayPrependAll(string $path, array $values, array|bool $options = [])

Prepend multiple values at once in an existing array.

arrayAppend(string $path, mixed $value, array|bool $options = [])

Append to an existing array, pushing the value to the back/last position in the array.

arrayAppendAll(string $path, array $values, array|bool $options = [])

Append multiple values at once in an existing array.

arrayInsert(string $path, mixed $value, array $options = [])

Insert into an existing array at a specific position

arrayInsertAll(string $path, array $values, array $options = [])

Insert multiple values at once in an existing array at a specified position.

arrayAddUnique(string $path, mixed $value, array|bool $options = [])

Insert a value in an existing array only if the value isn't already contained in the array (by way of string comparison).

counter(string $path, int $delta, array|bool $options = [])

Increment/decrement a numerical fragment in a JSON document.

withExpiry(mixed $expiry)

Change the expiry of the enclosing document as part of the mutation.

execute()

Perform several mutation operations inside a single existing JSON document.

Details

MutateInBuilder insert(string $path, mixed $value, array|bool $options = [])

Insert a fragment provided the last element of the path doesn't exists.

Parameters

string $path

the path where to insert a new dictionary value.

mixed $value

the new dictionary value to insert.

array|bool $options

the array with command modificators. The boolean value, controls "createPath" option. Supported values are:

  • "createPath" (default: false) true to create missing intermediary nodes.
  • "xattr" (default: false) if true, the path refers to a location within the document's extended attributes, not the document body.

Return Value

MutateInBuilder

modeDocument(int $mode)

Select mode for new full-document operations.

It defines behaviour of MutateInBuilder#upsert() method. The $mode could take one of three modes:

  • FULLDOC_REPLACE: complain when document does not exist
  • FULLDOC_INSERT: complain when document does exist
  • FULLDOC_UPSERT: unconditionally set value for the document

Parameters

int $mode

operation mode

MutateInBuilder upsert(string $path, mixed $value, array|bool $options = [])

Insert a fragment, replacing the old value if the path exists.

When only one argument supplied, the library will handle it as full-document upsert, and treat this argument as value. See MutateInBuilder#modeDocument()

Parameters

string $path

the path where to insert (or replace) a dictionary value

mixed $value

the new dictionary value to be applied.

array|bool $options

the array with command modificators. The boolean value, controls "createPath" option. Supported values are:

  • "createPath" (default: false) true to create missing intermediary nodes.
  • "xattr" (default: false) if true, the path refers to a location within the document's extended attributes, not the document body.

Return Value

MutateInBuilder

MutateInBuilder replace(string $path, mixed $value, array $options = [])

Replace an existing value by the given fragment

Parameters

string $path

the path where the value to replace is

mixed $value

the new value

array $options

the array with command modificators. Supported values are:

  • "xattr" (default: false) if true, the path refers to a location within the document's extended attributes, not the document body.

Return Value

MutateInBuilder

MutateInBuilder remove(string $path, array $options = [])

Remove an entry in a JSON document.

Scalar, array element, dictionary entry, whole array or dictionary, depending on the path.

Parameters

string $path

the path to remove

array $options

the array with command modificators. Supported values are:

  • "xattr" (default: false) if true, the path refers to a location within the document's extended attributes, not the document body.

Return Value

MutateInBuilder

MutateInBuilder arrayPrepend(string $path, mixed $value, array|bool $options = [])

Prepend to an existing array, pushing the value to the front/first position in the array.

Parameters

string $path

the path of the array

mixed $value

the value to insert at the front of the array

array|bool $options

the array with command modificators. The boolean value, controls "createPath" option. Supported values are:

  • "createPath" (default: false) true to create missing intermediary nodes.
  • "xattr" (default: false) if true, the path refers to a location within the document's extended attributes, not the document body.

Return Value

MutateInBuilder

MutateInBuilder arrayPrependAll(string $path, array $values, array|bool $options = [])

Prepend multiple values at once in an existing array.

Push all values in the collection's iteration order to the front/start of the array. For example given an array [A, B, C], prepending the values X and Y yields [X, Y, A, B, C] and not [[X, Y], A, B, C].

Parameters

string $path

the path of the array

array $values

the values to insert at the front of the array as individual elements

array|bool $options

the array with command modificators. The boolean value, controls "createPath" option. Supported values are:

  • "createPath" (default: false) true to create missing intermediary nodes.
  • "xattr" (default: false) if true, the path refers to a location within the document's extended attributes, not the document body.

Return Value

MutateInBuilder

MutateInBuilder arrayAppend(string $path, mixed $value, array|bool $options = [])

Append to an existing array, pushing the value to the back/last position in the array.

Parameters

string $path

the path of the array

mixed $value

the value to insert at the back of the array

array|bool $options

the array with command modificators. The boolean value, controls "createPath" option. Supported values are:

  • "createPath" (default: false) true to create missing intermediary nodes.
  • "xattr" (default: false) if true, the path refers to a location within the document's extended attributes, not the document body.

Return Value

MutateInBuilder

MutateInBuilder arrayAppendAll(string $path, array $values, array|bool $options = [])

Append multiple values at once in an existing array.

Push all values in the collection's iteration order to the back/end of the array. For example given an array [A, B, C], appending the values X and Y yields [A, B, C, X, Y] and not [A, B, C, [X, Y]].

Parameters

string $path

the path of the array

array $values

the values to individually insert at the back of the array

array|bool $options

the array with command modificators. The boolean value, controls "createPath" option. Supported values are:

  • "createPath" (default: false) true to create missing intermediary nodes.
  • "xattr" (default: false) if true, the path refers to a location within the document's extended attributes, not the document body.

Return Value

MutateInBuilder

MutateInBuilder arrayInsert(string $path, mixed $value, array $options = [])

Insert into an existing array at a specific position

Position denoted in the path, eg. "sub.array[2]".

Parameters

string $path

the path (including array position) where to insert the value

mixed $value

the value to insert in the array

array $options

the array with command modificators. Supported values are:

  • "xattr" (default: false) if true, the path refers to a location within the document's extended attributes, not the document body.

Return Value

MutateInBuilder

MutateInBuilder arrayInsertAll(string $path, array $values, array $options = [])

Insert multiple values at once in an existing array at a specified position.

Position denoted in the path, eg. "sub.array[2]"), inserting all values in the collection's iteration order at the given position and shifting existing values beyond the position by the number of elements in the collection.

For example given an array [A, B, C], inserting the values X and Y at position 1 yields [A, B, X, Y, C] and not [A, B, [X, Y], C].

Parameters

string $path

the path of the array

array $values

the values to insert at the specified position of the array, each value becoming an entry at or after the insert position.

array $options

the array with command modificators. Supported values are:

  • "xattr" (default: false) if true, the path refers to a location within the document's extended attributes, not the document body.

Return Value

MutateInBuilder

MutateInBuilder arrayAddUnique(string $path, mixed $value, array|bool $options = [])

Insert a value in an existing array only if the value isn't already contained in the array (by way of string comparison).

Parameters

string $path

the path to mutate in the JSON

mixed $value

the value to insert

array|bool $options

the array with command modificators. The boolean value, controls "createPath" option. Supported values are:

  • "createPath" (default: false) true to create missing intermediary nodes.
  • "xattr" (default: false) if true, the path refers to a location within the document's extended attributes, not the document body.

Return Value

MutateInBuilder

MutateInBuilder counter(string $path, int $delta, array|bool $options = [])

Increment/decrement a numerical fragment in a JSON document.

If the value (last element of the path) doesn't exist the counter is created and takes the value of the delta.

Parameters

string $path

the path to the counter (must be containing a number).

int $delta

the value to increment or decrement the counter by

array|bool $options

the array with command modificators. The boolean value, controls "createPath" option. Supported values are:

  • "createPath" (default: false) true to create missing intermediary nodes.
  • "xattr" (default: false) if true, the path refers to a location within the document's extended attributes, not the document body.

Return Value

MutateInBuilder

MutateInBuilder withExpiry(mixed $expiry)

Change the expiry of the enclosing document as part of the mutation.

Parameters

mixed $expiry

the new expiry to apply (or 0 to avoid changing the expiry)

Return Value

MutateInBuilder

DocumentFragment execute()

Perform several mutation operations inside a single existing JSON document.

Return Value

DocumentFragment