MongoClassClient

class MongoClassClient()

Parameters

  • default_db_name str

    • The 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 str

    • The 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 bool

    • Whether 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 bool

    • Whether 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 str

    • The 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 str

    • The 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.

  • If you're inserting multiple mongoclasses with insert_one=False and ordered=False, the provided mongoclasses will be mutated by setting a `_mongodb_id` attribute with the id coming from InsertManyResult after this method executes.

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 bool

    • Whether to call mongoclass.insert() on each mongoclass. Defaults to False. False means it would use Collection.insert_many() to insert all documents at once.

  • *args, **kwargs

    • To be passed onto Collection.insert_many() or mongoclass.insert()

Returns

  • Union[InsertOneResult, InsertManyResult, List[InsertOneResult]]

    • A InsertOneResult if the provided mongoclasses parameter is just a single mongoclass.

    • A InsertManyResult if the provided mongoclasses parameter is a list of mongoclasses and insert_one=False

    • A List[InsertOneResult] if the provided mongoclasses paramater is a list of mongoclasses and insert_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 dict

    • The raw document.

  • collection str

    • The collection this document belongs to.

  • database str

    • The database the raw document belongs to.

  • force_nested bool

    • Forcefully 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 str

    • The name of the database.

Returns

  • Union[pymongo.database.Database, mongita.database.Database]

    • The Database object of the underlying engine.

Last updated