---
title: Charmhub | Deploy MongoDB using Charmhub - The Open Operator Collection
description: Deploy the latest version of MongoDB on any cloud.
url: https://charmhub.io/mongodb/libraries/mongodb_provider
---

# MongoDB

[Canonical](https://charmhub.io/publisher/data-platform "View all packages from Canonical")

* [Canonical](https://charmhub.io/publisher/data-platform "View all packages from Canonical")
* [Databases](https://charmhub.io/?filter=databases)

Platform:

24.04

22.04

6/stable 265

```
juju deploy mongodb --channel 6/stable
```

[Learn to deploy on juju >](https://juju.is/docs/juju/manage-applications)

[Toggle side navigation](https://charmhub.io/mongodb/libraries/mongodb_provider#drawer)

## charms.mongodb.v1.mongodb\_provider

* [*Docstrings*Docstrings](https://charmhub.io/mongodb/libraries/mongodb_provider)
  [*Code*Source code](https://charmhub.io/mongodb/libraries/mongodb_provider/source-code)
* + Download

    Fetch library

    ```
    charmcraft fetch-lib charms.mongodb.v1.mongodb_provider
    ```

    [Download mongodb\_provider.py](https://charmhub.io/mongodb/libraries/mongodb_provider/download)
  + *Last updated* 12 Nov 2024
  + *Revision* Library version 1.16

In this class, we manage client database relations.

This class creates a user and database for each application relation
and expose needed information for client connection via fields in
external relation.

---

Index

* [class FailedToGetHostsError](https://charmhub.io/mongodb/libraries/mongodb_provider#failedtogethostserror)
* [class MongoDBProvider](https://charmhub.io/mongodb/libraries/mongodb_provider#mongodbprovider)
* + [def \_\_init\_\_(
    self,
    charm,
    substrate,
    relation\_name)](https://charmhub.io/mongodb/libraries/mongodb_provider#mongodbprovider-__init__)
* + [def pass\_sanity\_hook\_checks(
    self)](https://charmhub.io/mongodb/libraries/mongodb_provider#mongodbprovider-pass_sanity_hook_checks)
* + [def pass\_hook\_checks(
    self,
    event)](https://charmhub.io/mongodb/libraries/mongodb_provider#mongodbprovider-pass_hook_checks)
* + [def oversee\_users(
    self,
    departed\_relation\_id,
    event)](https://charmhub.io/mongodb/libraries/mongodb_provider#mongodbprovider-oversee_users)
* + [def remove\_users(
    self,
    users\_being\_managed,
    expected\_current\_users)](https://charmhub.io/mongodb/libraries/mongodb_provider#mongodbprovider-remove_users)
* + [def add\_users(
    self,
    users\_being\_managed,
    expected\_current\_users)](https://charmhub.io/mongodb/libraries/mongodb_provider#mongodbprovider-add_users)
* + [def update\_users(
    self,
    event,
    users\_being\_managed,
    expected\_current\_users)](https://charmhub.io/mongodb/libraries/mongodb_provider#mongodbprovider-update_users)
* + [def auto\_delete\_dbs(
    self,
    departed\_relation\_id)](https://charmhub.io/mongodb/libraries/mongodb_provider#mongodbprovider-auto_delete_dbs)
* + [def update\_app\_relation\_data(
    self)](https://charmhub.io/mongodb/libraries/mongodb_provider#mongodbprovider-update_app_relation_data)
* + [def get\_relation\_name(
    self)](https://charmhub.io/mongodb/libraries/mongodb_provider#mongodbprovider-get_relation_name)
* + [def remove\_all\_relational\_users(
    self)](https://charmhub.io/mongodb/libraries/mongodb_provider#mongodbprovider-remove_all_relational_users)

#### class FailedToGetHostsError

Description

Raised when charm fails to retrieve hosts. None

#### class MongoDBProvider

Description

In this class, we manage client database relations. None

Methods

MongoDBProvider.
\_\_init\_\_(

*self*

,
charm: CharmBase

,
substrate

,
relation\_name: str
)

Constructor for MongoDBProvider object.

Arguments

charm

the charm for which this relation is provided

substrate

host type, either "k8s" or "vm"

relation\_name

the name of the relation

MongoDBProvider.
pass\_sanity\_hook\_checks(

*self*
)

Description

Runs reusable and event agnostic checks. None

MongoDBProvider.
pass\_hook\_checks(

*self*

,
event: RelationEvent
)

Description

Runs the pre-hooks checks for MongoDBProvider, returns True if all pass. None

MongoDBProvider.
oversee\_users(

*self*

,
departed\_relation\_id

,
event
)

Oversees the users of the application.

Description

Function manages user relations by removing, updated, and creating
users; and dropping databases when necessary.

Args:
departed\_relation\_id: When specified execution of functions
makes sure to exclude the users and databases and remove
them if necessary.
event: relation event.

When the function is executed in relation departed event, the departed
relation is still on the list of all relations. Therefore, for proper
work of the function, we need to exclude departed relation from the list.

Raises:
PyMongoError

MongoDBProvider.
remove\_users(

*self*

,
users\_being\_managed

,
expected\_current\_users
)

Removes users from Charmed MongoDB.

Description

Note this only removes users that this application of Charmed MongoDB is responsible for
managing. It won't remove:

1. users created from other applications
2. users created from other mongos routers.

Raises:
PyMongoError

MongoDBProvider.
add\_users(

*self*

,
users\_being\_managed

,
expected\_current\_users
)

Adds users to Charmed MongoDB.

Description

Raises:
PyMongoError

MongoDBProvider.
update\_users(

*self*

,
event: EventBase

,
users\_being\_managed

,
expected\_current\_users
)

Updates existing users in Charmed MongoDB.

Description

Raises:
PyMongoError

MongoDBProvider.
auto\_delete\_dbs(

*self*

,
departed\_relation\_id
)

Delete's unused dbs if configured to do so.

Description

Raises:
PyMongoError

MongoDBProvider.
update\_app\_relation\_data(

*self*
)

Description

Helper function to update application relation data. None

MongoDBProvider.
get\_relation\_name(

*self*
)

Description

Returns the name of the relation to use. None

MongoDBProvider.
remove\_all\_relational\_users(

*self*
)

Removes all users from DB.

Description

Raises: PyMongoError.
