---
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/mongo
---

# 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/mongo#drawer)

## charms.mongodb.v0.mongo

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

    Fetch library

    ```
    charmcraft fetch-lib charms.mongodb.v0.mongo
    ```

    [Download mongo.py](https://charmhub.io/mongodb/libraries/mongo/download)
  + *Last updated* 04 Oct 2024
  + *Revision* Library version 0.2

Code for interactions with MongoDB.

---

Index

* [class NotReadyError](https://charmhub.io/mongodb/libraries/mongo#notreadyerror)
* [class AmbiguousConfigError](https://charmhub.io/mongodb/libraries/mongo#ambiguousconfigerror)
* [class MongoConfiguration](https://charmhub.io/mongodb/libraries/mongo#mongoconfiguration)
* + [def uri(
    self)](https://charmhub.io/mongodb/libraries/mongo#mongoconfiguration-uri)
* [def supported\_roles(
  config
  )](https://charmhub.io/mongodb/libraries/mongo#supported_roles)
* [class MongoConnection](https://charmhub.io/mongodb/libraries/mongo#mongoconnection)
* + [def \_\_init\_\_(
    self,
    config,
    uri,
    direct)](https://charmhub.io/mongodb/libraries/mongo#mongoconnection-__init__)
* + [def \_\_enter\_\_(
    self)](https://charmhub.io/mongodb/libraries/mongo#mongoconnection-__enter__)
* + [def \_\_exit\_\_(
    self,
    object\_type,
    value,
    traceback)](https://charmhub.io/mongodb/libraries/mongo#mongoconnection-__exit__)
* + [def is\_ready(
    self)](https://charmhub.io/mongodb/libraries/mongo#mongoconnection-is_ready)
* + [def create\_user(
    self,
    config)](https://charmhub.io/mongodb/libraries/mongo#mongoconnection-create_user)
* + [def update\_user(
    self,
    config)](https://charmhub.io/mongodb/libraries/mongo#mongoconnection-update_user)
* + [def set\_user\_password(
    self,
    username,
    password)](https://charmhub.io/mongodb/libraries/mongo#mongoconnection-set_user_password)
* + [def create\_role(
    self,
    role\_name,
    privileges,
    roles)](https://charmhub.io/mongodb/libraries/mongo#mongoconnection-create_role)
* + [def drop\_user(
    self,
    username)](https://charmhub.io/mongodb/libraries/mongo#mongoconnection-drop_user)
* + [def get\_users(
    self)](https://charmhub.io/mongodb/libraries/mongo#mongoconnection-get_users)
* + [def get\_databases(
    self)](https://charmhub.io/mongodb/libraries/mongo#mongoconnection-get_databases)
* + [def drop\_database(
    self,
    database)](https://charmhub.io/mongodb/libraries/mongo#mongoconnection-drop_database)

#### class NotReadyError

Description

Raised when not all replica set members healthy or finished initial sync. None

#### class AmbiguousConfigError

Description

Raised when the config could correspond to a mongod config or mongos config. None

#### class MongoConfiguration

Class for Mongo configurations usable my mongos and mongodb.

Description

— replset: name of replica set
— database: database name.
— username: username.
— password: password.
— hosts: full list of hosts to connect to, needed for the URI.
— tls\_external: indicator for use of internal TLS connection.
— tls\_internal: indicator for use of external TLS connection.

Methods

MongoConfiguration.
uri(

*self*
)

Description

Return URI concatenated from fields. None

#### def supported\_roles(config: MongoConfiguration)

Description

Return the supported roles for the given configuration. None

#### class MongoConnection

In this class we create connection object to Mongo[s/db].

Description

This class is meant for agnositc functions in mongos and mongodb.

Real connection is created on the first call to Mongo[s/db].
Delayed connectivity allows to firstly check database readiness
and reuse the same connection for an actual query later in the code.

Connection is automatically closed when object destroyed.
Automatic close allows to have more clean code.

Note that connection when used may lead to the following pymongo errors: ConfigurationError,
ConfigurationError, OperationFailure. It is suggested that the following pattern be adopted
when using MongoDBConnection:

with MongoMongos(MongoConfig) as mongo:
try:
mongo.<some operation from this class>
except ConfigurationError, OperationFailure:
<error handling as needed>

Methods

MongoConnection.
\_\_init\_\_(

*self*

,
config: MongoConfiguration

,
uri

,
direct
)

A MongoDB client interface.

Arguments

config

MongoDB Configuration object.

uri

allow using custom MongoDB URI, needed for replSet init.

direct

force a direct connection to a specific host, avoiding
reading replica set configuration and reconnection.

MongoConnection.
\_\_enter\_\_(

*self*
)

Description

Return a reference to the new connection. None

MongoConnection.
\_\_exit\_\_(

*self*

,
object\_type

,
value

,
traceback
)

Description

Disconnect from MongoDB client. None

MongoConnection.
is\_ready(

*self*
)

Is the MongoDB server ready for services requests.

Returns

True if services is ready False otherwise. Retries over a period of 60 seconds times to
allow server time to start up.

MongoConnection.
create\_user(

*self*

,
config: MongoConfiguration
)

Create user.

Description

Grant read and write privileges for specified database.

MongoConnection.
update\_user(

*self*

,
config: MongoConfiguration
)

Description

Update grants on database. None

MongoConnection.
set\_user\_password(

*self*

,
username

,
password: str
)

Description

Update the password. None

MongoConnection.
create\_role(

*self*

,
role\_name: str

,
privileges: dict

,
roles: dict
)

Creates a new role.

Arguments

role\_name

name of the role to be added.

privileges

privileges to be associated with the role.

roles

List of roles from which this role inherits privileges.

MongoConnection.
drop\_user(

*self*

,
username: str
)

Description

Drop user. None

MongoConnection.
get\_users(

*self*
)

Description

Add a new member to replica set config inside MongoDB. None

MongoConnection.
get\_databases(

*self*
)

Description

Return list of all non-default databases. None

MongoConnection.
drop\_database(

*self*

,
database: str
)

Description

Drop a non-default database. None
