mysqlx.Collection

class mysqlx.Collection(schema: Schema, name: str | bytes)

Bases: DatabaseObject

Represents a collection of documents on a schema.

Parameters:
  • schema (mysqlx.Schema) – The Schema object.

  • name (str) – The collection name.

add(*values: DbDoc) AddStatement

Adds a list of documents to a collection.

Parameters:

*values – The document list to be added into the collection.

Returns:

AddStatement object.

Return type:

mysqlx.AddStatement

add_or_replace_one(doc_id: str, doc: Dict | DbDoc) Result

Upserts the Document matching the document ID with a new document provided.

Parameters:
  • doc_id (str) – Document ID

  • doc (mysqlx.DbDoc or dict) – New Document

am_i_real() Any

Verifies if this object exists in the database.

Returns:

True if object exists in database.

Return type:

bool

Raises:

NotImplementedError – This method must be implemented.

Deprecated since version 8.0.12: Use exists_in_database() method instead.

count() int

Counts the documents in the collection.

Returns:

The total of documents in the collection.

Return type:

int

create_index(index_name: str, fields_desc: Dict[str, Any]) CreateCollectionIndexStatement

Creates a collection index.

Parameters:
  • index_name (str) – Index name.

  • fields_desc (dict) –

    A dictionary containing the fields members that constraints the index to be created. It must have the form as shown in the following:

    {"fields": [{"field": member_path,
                 "type": member_type,
                 "required": member_required,
                 "array": array,
                 "collation": collation,
                 "options": options,
                 "srid": srid},
                 # {... more members,
                 #      repeated as many times
                 #      as needed}
                 ],
     "type": type}
    

drop_index(index_name: str) None

Drops a collection index.

Parameters:

index_name (str) – Index name.

exists_in_database() bool

Verifies if this object exists in the database.

Returns:

True if object exists in database.

Return type:

bool

find(condition: str | None = None) FindStatement

Retrieves documents from a collection.

Parameters:

condition (Optional[str]) – The string with the filter expression of the documents to be retrieved.

get_connection() Connection

Returns the underlying connection.

Returns:

The connection object.

Return type:

mysqlx.connection.Connection

get_name() str

Returns the name of this database object.

Returns:

The name of this database object.

Return type:

str

get_one(doc_id: str) DbDoc

Returns a Document matching the Document ID.

Parameters:

doc_id (str) – Document ID

Returns:

The Document matching the Document ID.

Return type:

mysqlx.DbDoc

get_schema() Schema

Returns the Schema object of this database object.

Returns:

The Schema object.

Return type:

mysqlx.Schema

get_session() Session

Returns the session of this database object.

Returns:

The Session object.

Return type:

mysqlx.Session

modify(condition: str) ModifyStatement

Modifies documents based on the condition.

Parameters:

condition (str) – The string with the filter expression of the documents to be modified.

Returns:

ModifyStatement object.

Return type:

mysqlx.ModifyStatement

Changed in version 8.0.12: The condition parameter is now mandatory.

property name: str

The name of this database object.

Type:

str

remove(condition: str) RemoveStatement

Removes documents based on the condition.

Parameters:

condition (str) – The string with the filter expression of the documents to be removed.

Returns:

RemoveStatement object.

Return type:

mysqlx.RemoveStatement

Changed in version 8.0.12: The condition parameter is now mandatory.

remove_one(doc_id: str) Result

Removes a Document matching the Document ID.

Parameters:

doc_id (str) – Document ID

Returns:

Result object.

Return type:

mysqlx.Result

replace_one(doc_id: str, doc: Dict | DbDoc) Result

Replaces the Document matching the document ID with a new document provided.

Parameters:
  • doc_id (str) – Document ID

  • doc (mysqlx.DbDoc or dict) – New Document

property schema: Schema

The Schema object.

Type:

mysqlx.Schema

property session: Session

The Session object.

Type:

mysqlx.Session

who_am_i() str

Returns the name of this database object.

Returns:

The name of this database object.

Return type:

str

Deprecated since version 8.0.12: Use get_name() method instead.