MongoClassClient
class MongoClassClient()Parameters
default_db_namestrThe name of the default database.
*args, **kwargsTo 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
collectionstrThe collection the class must map to. Defaults to the name of the class but lowered.
databaseUnion[str, Database]The database to use. Defaults to the default database.
insert_on_initboolWhether to automatically insert a mongoclass into mongodb whenever a mongoclass instance is created. Defaults to False. This is the equivalent of passing
_insert=Trueevery time you create a mongoclass instance. This can also be overwritten by setting_insert=False
nestedboolWhether 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
collectionstrThe collection to use.
*argsArguments to pass onto
find_one
databaseUnion[str, Database]The database to use. Defaults to the default database.
**kwargsKeyword 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
) -> CursorParameters
collectionstrThe collection to use.
*argsArguments to pass onto
find
databaseUnion[str, Database]The database to use. Defaults to the default database.
**kwargsKeyword arguments to pass onto
find
Returns
CursorA 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
mongoclassesUnion[object, List[object]]A list of mongoclasses or a single mongoclass. When inserting a single mongoclass, you can just do
mongoclass.insert()
insert_oneboolWhether to call
mongoclass.insert()on each mongoclass. Defaults toFalse. False means it would useCollection.insert_many()to insert all documents at once.
*args, **kwargsTo be passed onto
Collection.insert_many()ormongoclass.insert()
Returns
Union[InsertOneResult, InsertManyResult, List[InsertOneResult]]A
InsertOneResultif the providedmongoclassesparameter is just a single mongoclass.A
InsertManyResultif the providedmongoclassesparameter is a list of mongoclasses andinsert_one=FalseA
List[InsertOneResult]if the providedmongoclassesparamater is a list of mongoclasses andinsert_one=True
def map_document(self,
data: dict,
collection: str,
database: str,
force_nested: bool = False
) -> objectMap 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
datadictThe raw document.
collectionstrThe collection this document belongs to.
databasestrThe database the raw document belongs to.
force_nestedboolForcefully 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
objectThe 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
databasestrThe name of the database.
Returns
Union[pymongo.database.Database, mongita.database.Database]The
Databaseobject of the underlying engine.
Last updated