MySQL Shell  8.0.16
Unified development interface for MySQL Products
List of all members
CollectionAdd Class Reference

Handler for document addition on a Collection. More...

Methods

CollectionAdd add (DocDefinition document[, DocDefinition document,...])
 Adds documents into a collection. More...
 
CollectionAdd add (List documents)
 Adds documents into a collection. More...
 
Result execute ()
 Executes the add operation, the documents are added to the target collection. More...
 

Detailed Description

Handler for document addition on a Collection.

This object provides the necessary functions to allow adding documents into a collection.

This object should only be created by calling any of the add functions on the collection object where the documents will be added.

See also
Collection

Member Function Documentation

◆ add() [1/2]

CollectionAdd add ( DocDefinition  document[, DocDefinition document,...])

Adds documents into a collection.

Parameters

  • document The definition of a document to be added.
  • documents A list of documents to be added.
Returns
This CollectionAdd object.

This function receives one or more document definitions to be added into a collection.

A document definition may be provided in two ways:

  • Using a dictionary containing the document fields.
  • Using A JSON string as a document expression.

There are three ways to add multiple documents:

  • Passing several parameters to the function, each parameter should be a document definition.
  • Passing a list of document definitions.
  • Calling this function several times before calling execute().

To be added, every document must have a string property named '_id' ideally with a universal unique identifier (UUID) as value. If the '_id' property is missing, it is automatically set with an internally generated UUID.

Method Chaining

This method can be called many times, every time it is called the received document(s) will be cached into an internal list. The actual addition into the collection will occur only when the execute method is called.

◆ add() [2/2]

CollectionAdd add ( List  documents)

Adds documents into a collection.

Parameters

  • document The definition of a document to be added.
  • documents A list of documents to be added.
Returns
This CollectionAdd object.

This function receives one or more document definitions to be added into a collection.

A document definition may be provided in two ways:

  • Using a dictionary containing the document fields.
  • Using A JSON string as a document expression.

There are three ways to add multiple documents:

  • Passing several parameters to the function, each parameter should be a document definition.
  • Passing a list of document definitions.
  • Calling this function several times before calling execute().

To be added, every document must have a string property named '_id' ideally with a universal unique identifier (UUID) as value. If the '_id' property is missing, it is automatically set with an internally generated UUID.

Method Chaining

This method can be called many times, every time it is called the received document(s) will be cached into an internal list. The actual addition into the collection will occur only when the execute method is called.

◆ execute()

Result execute ( )

Executes the add operation, the documents are added to the target collection.

Returns
A Result object.

Method Chaining

This function can be invoked once after:

  • add(DocDefinition document[, DocDefinition document, ...])

Examples

Using a Document List

Adding document using an existing document list

var result = collection.add([{ "name": 'my sexth', "passed": 'again', "count": 5 }, mysqlx.expr('{"name": "my senevth", "passed": "yep again", "count": 5}')]).execute()
print("Affected Rows Mixed List:", result.affectedItemsCount, "\n")

Multiple Parameters

Adding document using a separate parameter for each document on a single call to add(...)

var result = collection.add({ "name": 'my eigth', "passed": 'yep', "count": 6 }, mysqlx.expr('{"name": "my nineth", "passed": "yep again", "count": 6}')).execute()
print("Affected Rows Multiple Params:", result.affectedItemsCount, "\n")

Chaining Addition

Adding documents using chained calls to add(...)

var result = collection.add({ name: 'my fourth', passed: 'again', count: 4 }).add({ name: 'my fifth', passed: 'once again', count: 5 }).execute();
print("Affected Rows Chained:", result.affectedItemsCount, "\n");

JSON as Document Expressions

A document can be represented as a JSON expression as follows:

var result = collection.add(mysqlx.expr('{"name": "my fifth", "passed": "document", "count": 1}')).execute()
print("Affected Rows Single Expression:", result.affectedItemsCount, "\n")