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 theMongo
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 anDocument.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 useObjectID
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
andread_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 itsuse()
method called to set a db, anAttributeError
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