mysqlx.Collection

class mysqlx.Collection(schema, name)

Bases: mysqlx.crud.DatabaseObject

Represents a collection of documents on a schema.

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

  • name (str) – The collection name.

add(*values)

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, doc)

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()

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()

Counts the documents in the collection.

Returns

The total of documents in the collection.

Return type

int

create_index(index_name, fields_desc)

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)

Drops a collection index.

Parameters

index_name (str) – Index name.

exists_in_database()

Verifies if this object exists in the database.

Returns

True if object exists in database.

Return type

bool

find(condition=None)

Retrieves documents from a collection.

Parameters

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

get_connection()

Returns the underlying connection.

Returns

The connection object.

Return type

mysqlx.connection.Connection

get_name()

Returns the name of this database object.

Returns

The name of this database object.

Return type

str

get_one(doc_id)

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()

Returns the Schema object of this database object.

Returns

The Schema object.

Return type

mysqlx.Schema

get_session()

Returns the session of this database object.

Returns

The Session object.

Return type

mysqlx.Session

modify(condition)

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

The name of this database object.

Type

str

remove(condition)

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)

Removes a Document matching the Document ID.

Parameters

doc_id (str) – Document ID

Returns

Result object.

Return type

mysqlx.Result

replace_one(doc_id, doc)

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

The Schema object.

Type

mysqlx.Schema

property session

The Session object.

Type

mysqlx.Session

who_am_i()

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.