Module motor_odm.document

This module contains the base class for interacting with Motor-ODM: Document. The Document class is the main entry point to Motor-ODM and provides its main interface.

class motor_odm.document.DocumentMetaclass

Bases: pydantic.main.ModelMetaclass

The meta class for Document. Ensures that the Mongo class is automatically inherited.

class motor_odm.document.Document

Bases: pydantic.main.BaseModel

This is the base class for all documents defined using Motor-ODM.

A Document is a pydantic model that can be inserted into a MongoDB collection. This class provides an easy interface for interacting with the database. Each document has an Document.id (named _id in MongoDB) by default by which it can be uniquely identified in the database. The name of this field cannot be customized however you can override it if you don’t want to use ObjectID values for your IDs.

classmethod all(db_filter: Query = None, **kwargs: Any) → AsyncIterator[GenericDocument]

Returns multiple documents from the collection.

This method is filterable.

async classmethod batch_insert(*objects: GenericDocument)None

Inserts multiple documents at once.

It is preferred to use this method over multiple insert() calls as the performance can be much better.

classmethod collection() → motor.core.AgnosticCollection

Returns the collection for this Document.

The collection uses the codec_options, read_preference, write_concern and read_concern from the document’s `Mongo` class.

async classmethod count(db_filter: Query = None, **kwargs: Any)int

Returns the number of documents in this class’s collection.

This method is filterable.

classmethod db() → motor.core.AgnosticDatabase

Returns the database that is currently associated with this document.

If no such database exists this returns the database of the parent document (its superclass). If no Document class had its use() method called to set a db, an AttributeError is raised.

document(*, include: Union[AbstractSetIntStr, DictIntStrAny] = None, exclude: Union[AbstractSetIntStr, DictIntStrAny] = None) → DictStrAny

Converts this object into a dictionary suitable to be saved to MongoDB.

classmethod find(db_filter: Query = None, **kwargs: Any) → AsyncIterator[GenericDocument]

Returns multiple documents from the collection.

This method is filterable.

async classmethod get(db_filter: Query = None, **kwargs: Any) → Optional[GenericDocument]

Returns a single document from the collection.

This method is filterable.

async insert()None

Inserts the object into the database.

The object is inserted as a new object.

async reload()None

Reloads a document from the database.

Use this method if a model might have changed in the database and you need to retrieve the current version. You do not need to call this after inserting a newly created object into the database.

classmethod use(db: motor.core.AgnosticDatabase)None

Sets the database to be used by this Document.

The database will also be used by subclasses of this class unless they use() their own database.

This method has to be invoked before the ODM class can be used.

id: ObjectId = None

The document’s ID in the database.

By default this field is of type ObjectId but it can be overridden to supply your own ID types. Note that if you intend to override this field you must set its alias to _id in order for your IDs to be recognized as such by MongoDB.