MongoClassClient
class MongoClassClient()
Parameters
default_db_name
strThe name of the default database.
*args, **kwargs
To be passed onto
MongoClient()
Methods
def mongoclass(self,
collection: str = None,
database: Union[str, Database] = None,
insert_on_init: bool = False,
nested: bool = False
)
A decorator used to map a dataclass onto a collection. In other words, it converts the dataclass onto a mongoclass.
Parameters
collection
strThe collection the class must map to. Defaults to the name of the class but lowered.
database
Union[str, Database]The database to use. Defaults to the default database.
insert_on_init
boolWhether to automatically insert a mongoclass into mongodb whenever a mongoclass instance is created. Defaults to False. This is the equivalent of passing
_insert=True
every time you create a mongoclass instance. This can also be overwritten by setting_insert=False
nested
boolWhether this mongoclass has other mongoclasses inside it. Nesting is not automatically determined for performance purposes. Defaults to False.
def find_class(
self,
collection: str,
*args,
database: Union[str, Database] = None,
**kwargs
) -> Optional[object]
Find a single document and convert it onto a mongoclass that maps to the collection of the document.
Parameters
collection
strThe collection to use.
*args
Arguments to pass onto
find_one
database
Union[str, Database]The database to use. Defaults to the default database.
**kwargs
Keyword arguments to pass onto
find_one
Returns
Optional[object]
The mongoclass containing the document's data if it exists, otherwise,
None
.
def find_classes(
self,
collection: str,
*args,
database: Union[str, Database] = None,
**kwargs
) -> Cursor
Parameters
collection
strThe collection to use.
*args
Arguments to pass onto
find
database
Union[str, Database]The database to use. Defaults to the default database.
**kwargs
Keyword arguments to pass onto
find
Returns
Cursor
A mongoclass cursor.
def insert_classes(
self,
mongoclasses: Union[object, List[object]],
*args,
**kwargs
) -> Union[InsertOneResult, InsertManyResult, List[InsertOneResult]]
Insert a mongoclass or a list of mongoclasses into its respective collection and database. This method can accept mongoclasses with different collections and different databases as long as insert_one
is True
.
Parameters
mongoclasses
Union[object, List[object]]A list of mongoclasses or a single mongoclass. When inserting a single mongoclass, you can just do
mongoclass.insert()
insert_one
boolWhether to call
mongoclass.insert()
on each mongoclass. Defaults toFalse
. False means it would useCollection.insert_many()
to insert all documents at once.
*args, **kwargs
To be passed onto
Collection.insert_many()
ormongoclass.insert()
Returns
Union[InsertOneResult, InsertManyResult, List[InsertOneResult]]
A
InsertOneResult
if the providedmongoclasses
parameter is just a single mongoclass.A
InsertManyResult
if the providedmongoclasses
parameter is a list of mongoclasses andinsert_one=False
A
List[InsertOneResult]
if the providedmongoclasses
paramater is a list of mongoclasses andinsert_one=True
def map_document(self,
data: dict,
collection: str,
database: str,
force_nested: bool = False
) -> object
Map a raw document into a mongoclass. It's rare you'll call this method manually. This is a internal method used by find_class
and find_classes
.
Parameters
data
dictThe raw document.
collection
strThe collection this document belongs to.
database
strThe database the raw document belongs to.
force_nested
boolForcefully tell mongoclass that this document is a nested document and it contains other mongoclasses inside it. Defaults to False. Usually this parameter is only set in a recursive manner.
Returns
object
The mongoclass object containing the data
def get_db(
self,
database: str
) -> Union[pymongo.database.Database, mongita.database.Database]
Get a database. Equivalent to client["database"]
. This method exists simply because type hinting seems to be broken, nothing more.
Parameters
database
strThe name of the database.
Returns
Union[pymongo.database.Database, mongita.database.Database]
The
Database
object of the underlying engine.
Last updated