[docs]classDatabase:""" Represents a MongoDB database, providing access to collections and database-level operations. """def__init__(self,client,name:str)->None:""" Initializes a Database instance. Args: client: The client instance managing the connection to the MongoDB server. name (str): The name of the database. """self._name=nameself._client=clientdef__getattr__(self,name:str)->Collection:""" Dynamically accesses a collection by name. Args: name (str): The name of the collection to access. Returns: Collection: The collection instance for the specified name. """returnCollection(self,name)@propertydefname(self):""" The name of the database. Returns: str: The name of the database. """returnself._namedef_get_connection(self)->AsyncMongoConnection:""" Retrieves the connection instance associated with the database. Returns: AsyncMongoConnection: The connection instance. """returnself._client.connection
[docs]asyncdeflist_collection_names(self)->list[str]:""" Lists all collection names in the database. Returns: list[str]: A list of collection names. Raises: Any exception that occurs during the command execution. """conn=self._get_connection()_cmd={"listCollections":1,"cursor":{}}resp=awaitconn.command(self._name,_cmd)names=[item["name"]foriteminresp["cursor"]["firstBatch"]]returnnames