MySQL Shell API  8.0.17
Unified development interface for MySQL Products
Methods | List of all members
CollectionModify Class Reference

Operation to update documents on a Collection. More...

Methods

CollectionModify modify (String searchCondition)
 Sets the search condition to identify the Documents to be updated on the owner Collection. More...
 
CollectionModify set (String attribute, Value value)
 Sets or updates attributes on documents in a collection. More...
 
CollectionModify unset (List attributes)
 Removes attributes from documents in a collection. More...
 
CollectionModify merge (Document document)
 Adds attributes taken from a document into the documents in a collection. More...
 
CollectionModify patch (Document document)
 Performs modifications on a document based on a patch JSON object. More...
 
CollectionModify arrayAppend (String docPath, Value value)
 Appends a value into an array attribute in documents of a collection. More...
 
CollectionModify arrayInsert (String docPath, Value value)
 Inserts a value into a specific position in an array attribute in documents of a collection. More...
 
CollectionModify arrayDelete (String docPath)
 Deletes the value at a specific position in an array attribute in documents of a collection. More...
 
CollectionModify sort (List sortExprStr)
 Sets the document order in which the update operations added to the handler should be done. More...
 
CollectionModify limit (Integer numberOfRows)
 Sets a limit for the documents to be updated by the operations added to the handler. More...
 
CollectionFind bind (String name, Value value)
 Binds a value to a specific placeholder used on this CollectionModify object. More...
 
Result execute ()
 Executes the update operations added to the handler with the configured filter and limit. More...
 
CollectionModify unset (String attribute[, String attribute,...])
 Removes attributes from documents in a collection. More...
 

Detailed Description

Operation to update documents on a Collection.

A CollectionModify object represents an operation to update documents on a Collection, it is created through the modify function on the Collection class.

See also
Collection

Member Function Documentation

◆ modify()

CollectionModify modify ( String  searchCondition)

Sets the search condition to identify the Documents to be updated on the owner Collection.

Parameters
searchConditionAn expression to identify the documents to be updated.

Creates a handler to update documents in the collection.

Returns
This CollectionModify object.

A condition must be provided to this function, all the documents matching the condition will be updated.

To update all the documents, set a condition that always evaluates to true, for example '1'.

Method Chaining

This function is called automatically when Collection.modify(searchCondition) is called.

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().
Collection

◆ set()

CollectionModify set ( String  attribute,
Value  value 
)

Sets or updates attributes on documents in a collection.

Parameters
attributeA string with the document path of the item to be set.
valueThe value to be set on the specified attribute.
Returns
This CollectionModify object.

Adds an operation into the modify handler to set an attribute on the documents that were included on the selection filter and limit.

  • If the attribute is not present on the document, it will be added with the given value.
  • If the attribute already exists on the document, it will be updated with the given value.

Using Expressions for Values

The received values are set into the document in a literal way unless an expression is used.

When an expression is used, it is evaluated on the server and the resulting value is set into the document.

To define an expression use:

mysqlx.expr(expression)

The expression also can be used for Parameter Binding.

The attribute addition will be done on the collection's documents once the execute method is called.

Method Chaining

This function can be invoked multiple times after:

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ unset() [1/2]

CollectionModify unset ( String  attribute[, String attribute,...])

Removes attributes from documents in a collection.

Parameters
attributeA string with the document path of the attribute to be removed.
Returns
This CollectionModify object.

The attribute removal will be done on the collection's documents once the execute method is called.

Method Chaining

This function can be invoked multiple times after:

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ unset() [2/2]

CollectionModify unset ( List  attributes)

Removes attributes from documents in a collection.

Parameters
attributesA list with the document paths of the attributes to be removed.
Returns
This CollectionModify object.

For each attribute on the attributes list, adds an operation into the modify handler to remove the attribute on the documents that were included on the selection filter and limit.

The attribute removal will be done on the collection's documents once the execute method is called.

Method Chaining

This function can be invoked multiple times after:

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ merge()

CollectionModify merge ( Document  document)

Adds attributes taken from a document into the documents in a collection.

Parameters
documentThe document from which the attributes will be merged.
Returns
This CollectionModify object.

This function adds an operation to add into the documents of a collection, all the attributes defined in document that do not exist on the collection's documents.

The attribute addition will be done on the collection's documents once the execute method is called.

Attention
This function will be removed in a future release, use the patch function instead.

Method Chaining

This function can be invoked multiple times after:

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ patch()

CollectionModify patch ( Document  document)

Performs modifications on a document based on a patch JSON object.

Parameters
documentThe JSON object to be used on the patch process.
Returns
This CollectionModify object.

This function adds an operation to update the documents of a collection, the patch operation follows the algorithm described on the JSON Merge Patch RFC7386.

The patch JSON object will be used to either add, update or remove fields from documents in the collection that match the filter specified on the call to the modify() function.

The operation to be performed depends on the attributes defined at the patch JSON object:

  • Any attribute with value equal to null will be removed if exists.
  • Any attribute with value different than null will be updated if exists.
  • Any attribute with value different than null will be added if does not exists.

Special considerations:

  • The _id of the documents is immutable, so it will not be affected by the patch operation even if it is included on the patch JSON object.
  • The patch JSON object accepts expression objects as values. If used they will be evaluated at the server side.

The patch operations will be done on the collection's documents once the execute method is called.

Method Chaining

This function can be invoked multiple times after:

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ arrayAppend()

CollectionModify arrayAppend ( String  docPath,
Value  value 
)

Appends a value into an array attribute in documents of a collection.

Parameters
docPathA document path that identifies the array attribute where the value will be appended.
valueThe value to be appended.
Returns
This CollectionModify object.

Adds an operation into the modify handler to append a value into an array attribute on the documents that were included on the selection filter and limit.

Method Chaining

This function can be invoked multiple times after:

The attribute addition will be done on the collection's documents once the execute method is called.

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ arrayInsert()

CollectionModify arrayInsert ( String  docPath,
Value  value 
)

Inserts a value into a specific position in an array attribute in documents of a collection.

Parameters
docPathA document path that identifies the array attribute and position where the value will be inserted.
valueThe value to be inserted.
Returns
This CollectionModify object.

Adds an operation into the modify handler to insert a value into an array attribute on the documents that were included on the selection filter and limit.

The insertion of the value will be done on the collection's documents once the execute method is called.

Method Chaining

This function can be invoked multiple times after:

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ arrayDelete()

CollectionModify arrayDelete ( String  docPath)

Deletes the value at a specific position in an array attribute in documents of a collection.

Parameters
docPathA document path that identifies the array attribute and position of the value to be deleted.
Returns
This CollectionModify object.

Adds an operation into the modify handler to delete a value from an array attribute on the documents that were included on the selection filter and limit.

The attribute deletion will be done on the collection's documents once the execute method is called.

Attention
This function will be removed in a future release, use the unset function instead.

Method Chaining

This function can be invoked multiple times after:

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ sort()

CollectionModify sort ( List  sortExprStr)

Sets the document order in which the update operations added to the handler should be done.

Returns
This CollectionModify object.

The elements of sortExprStr list are usually strings defining the attribute name on which the collection sorting will be based. Each criterion could be followed by asc or desc to indicate ascending or descending order respectively. If no order is specified, ascending will be used by default.

This method is usually used in combination with limit to fix the amount of documents to be updated.

Method Chaining

This function can be invoked only once after:

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ limit()

CollectionModify limit ( Integer  numberOfDocs)

Sets a limit for the documents to be updated by the operations added to the handler.

Parameters
numberOfDocsthe number of documents to affect on the update operations.
Returns
This CollectionModify object.

This method is usually used in combination with sort to fix the amount of documents to be updated.

Method Chaining

This function can be invoked only once after:

This function can be called every time the statement is executed.

After this function invocation, the following functions can be invoked:

See also
Usage examples at execute().

◆ bind()

CollectionFind bind ( String  name,
Value  value 
)

Binds a value to a specific placeholder used on this CollectionModify object.

Parameters
nameThe name of the placeholder to which the value will be bound.
valueThe value to be bound on the placeholder.
Returns
This CollectionModify object.

Method Chaining

This function can be invoked multiple times right before calling execute:

After this function invocation, the following functions can be invoked:

An error will be raised if the placeholder indicated by name does not exist.

This function must be called once for each used placeholder or an error will be raised when the execute method is called.

See also
Usage examples at execute().

◆ execute()

shcore::Value execute ( )

Executes the update operations added to the handler with the configured filter and limit.

Returns
CollectionResultset A Result object that can be used to retrieve the results of the update operation.

Method Chaining

This function can be invoked after any other function on this class except modify().

The update operation will be executed in the order they were added.

Examples

//@# CollectionModify: Set Execution
var result = collection.modify('name = "brian"').set('alias', 'bri').set('last_name', 'black').set('age', mysqlx.expr('13+1')).execute();
print('Set Affected Rows:', result.affectedItemsCount, '\n');
var result = collection.find('name = "brian"').execute();
var doc = result.fetchOne();
print(dir(doc));
//@# CollectionModify: Set Execution Binding Array
var result = collection.modify('name = "brian"').set('hobbies', mysqlx.expr(':list')).bind('list', ['soccer', 'dance', 'read']).execute();
print('Set Affected Rows:', result.affectedItemsCount, '\n');
var result = collection.find('name = "brian"').execute();
var doc = result.fetchOne();
print(dir(doc));
print(doc.hobbies[0]);
print(doc.hobbies[1]);
print(doc.hobbies[2]);
//@ CollectionModify: Simple Unset Execution
var result = collection.modify('name = "brian"').unset('last_name').execute();
print('Unset Affected Rows:', result.affectedItemsCount, '\n');
var result = collection.find('name = "brian"').execute();
var doc = result.fetchOne();
print(dir(doc));
//@ CollectionModify: List Unset Execution
var result = collection.modify('name = "brian"').unset(['alias', 'age']).execute();
print('Unset Affected Rows:', result.affectedItemsCount, '\n');
var result = collection.find('name = "brian"').execute();
var doc = result.fetchOne();
print(dir(doc));
//@ CollectionModify: Merge Execution
var result = collection.modify('name = "brian"').merge({ last_name: 'black', age: 15, alias: 'bri', girlfriends: ['martha', 'karen'] }).execute();
print('Merge Affected Rows:', result.affectedItemsCount, '\n');
var result = collection.find('name = "brian"').execute();
var doc = result.fetchOne();
print("Brian's last_name:", doc.last_name, '\n');
print("Brian's age:", doc.age, '\n');
print("Brian's alias:", doc.alias, '\n');
print("Brian's first girlfriend:", doc.girlfriends[0], '\n');
print("Brian's second girlfriend:", doc.girlfriends[1], '\n');
//@ CollectionModify: arrayAppend Execution
var result = collection.modify('name = "brian"').arrayAppend('girlfriends', 'cloe').execute();
print('Array Append Affected Rows:', result.affectedItemsCount, '\n');
var result = collection.find('name = "brian"').execute();
var doc = result.fetchOne();
print("Brian's girlfriends:", doc.girlfriends.length);
print("Brian's last:", doc.girlfriends[2]);
//@ CollectionModify: arrayInsert Execution
var result = collection.modify('name = "brian"').arrayInsert('girlfriends[1]', 'samantha').execute();
print('Array Insert Affected Rows:', result.affectedItemsCount, '\n');
var result = collection.find('name = "brian"').execute();
var doc = result.fetchOne();
print("Brian's girlfriends:", doc.girlfriends.length, '\n');
print("Brian's second:", doc.girlfriends[1], '\n');
//@ CollectionModify: arrayDelete Execution
var result = collection.modify('name = "brian"').arrayDelete('girlfriends[2]').execute();
print('Array Delete Affected Rows:', result.affectedItemsCount, '\n');
var result = collection.find('name = "brian"').execute();
var doc = result.fetchOne();
print("Brian's girlfriends:", doc.girlfriends.length, '\n');
print("Brian's third:", doc.girlfriends[2], '\n');
//@ CollectionModify: sorting and limit Execution
var result = collection.modify('age = 15').set('sample', 'in_limit').sort(['name']).limit(2).execute();
print('Affected Rows:', result.affectedItemsCount, '\n');
var result = collection.find('age = 15').sort(['name']).execute();
//@ CollectionModify: sorting and limit Execution - 1
var doc = result.fetchOne();
print(dir(doc));
//@ CollectionModify: sorting and limit Execution - 2
var doc = result.fetchOne();
print(dir(doc));
//@ CollectionModify: sorting and limit Execution - 3
var doc = result.fetchOne();
print(dir(doc));
//@ CollectionModify: sorting and limit Execution - 4
var doc = result.fetchOne();
print(dir(doc));