MongoDB

Canonical Publisher

Platform:

Ubuntu
24.04 22.04
Channel Revision Published Runs on
6/stable 265 23 Jan 2026
Ubuntu 22.04
6/candidate 265 23 Jan 2026
Ubuntu 22.04
6/beta 265 23 Jan 2026
Ubuntu 22.04
6/edge 348 16 Jul 2026
Ubuntu 22.04
8-transition/edge 350 16 Jul 2026
Ubuntu 24.04
8-transition/edge 349 16 Jul 2026
Ubuntu 24.04
8/stable 266 23 Jan 2026
Ubuntu 24.04
8/candidate 266 23 Jan 2026
Ubuntu 24.04
8/beta 266 23 Jan 2026
Ubuntu 24.04
8/edge 358 Yesterday
Ubuntu 24.04
8/edge 357 Yesterday
Ubuntu 24.04
juju deploy mongodb --channel 6/stable

charms.mongodb.v1.mongodb

Code for interactions with MongoDB.


class FailedToMovePrimaryError

Description

Raised when attempt to move a primary fails. None

class MongoDBConnection

In this class we create connection object to MongoDB.

Description

Real connection is created on the first call to MongoDB. 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 MongoDBConnection(self._mongodb_config) as mongo: try: mongo.<some operation from this class> except ConfigurationError, ConfigurationError, OperationFailure: <error handling as needed>

Methods

MongoDBConnection. __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.

MongoDBConnection. init_replset( self )

Create replica set config the first time.

Description

Raises: ConfigurationError, ConfigurationError, OperationFailure

MongoDBConnection. get_replset_status( self )

Get a replica set status as a dict.

Description

Returns: A set of the replica set members along with their status.

Raises: ConfigurationError, ConfigurationError, OperationFailure

MongoDBConnection. get_replset_members( self )

Get a replica set members.

Description

Returns: A set of the replica set members as reported by mongod.

Raises: ConfigurationError, ConfigurationError, OperationFailure

MongoDBConnection. add_replset_member( self , hostname: str )

Add a new member to replica set config inside MongoDB.

Description

Raises: ConfigurationError, ConfigurationError, OperationFailure, NotReadyError

MongoDBConnection. remove_replset_member( self , hostname: str )

Remove member from replica set config inside MongoDB.

Description

Raises: ConfigurationError, ConfigurationError, OperationFailure, NotReadyError

MongoDBConnection. step_down_primary( self )

Description

Steps down the current primary, forcing a re-election. None

MongoDBConnection. move_primary( self , new_primary_ip: str )

Forcibly moves the primary to the new primary provided.

Arguments

new_primary_ip

ip address of the unit chosen to be the new primary.

MongoDBConnection. set_replicaset_election_priority( self , priority: int , ignore_member: str )

Description

Set the election priority for the entire replica set. None

MongoDBConnection. primary( self )

Description

Returns primary replica host. None

MongoDBConnection. is_any_sync( rs_status: Dict )

Returns true if any replica set members are syncing data.

Arguments

rs_status

current state of replica set as reported by mongod.

Description

Checks if any members in replica set are syncing data. Note it is recommended to run only one sync in the cluster to not have huge performance degradation.