Charmed MongoDB

Channel Revision Published Runs on
6/stable 164 26 Mar 2024
Ubuntu 22.04
6/candidate 164 26 Mar 2024
Ubuntu 22.04
6/beta 164 26 Mar 2024
Ubuntu 22.04
6/edge 173 27 May 2024
Ubuntu 22.04
5/stable 117 20 Apr 2023
Ubuntu 22.04
5/candidate 117 20 Apr 2023
Ubuntu 22.04
5/edge 139 21 Nov 2023
Ubuntu 22.04 Ubuntu 20.04
5/edge 109 06 Mar 2023
Ubuntu 22.04 Ubuntu 20.04
3.6/stable 100 28 Apr 2023
Ubuntu 20.04 Ubuntu 18.04
3.6/candidate 100 13 Apr 2023
Ubuntu 20.04 Ubuntu 18.04
3.6/edge 100 03 Feb 2023
Ubuntu 20.04 Ubuntu 18.04
juju deploy mongodb --channel 6/stable
Show information


22.04 20.04 18.04


Code for interactions with MongoDB.

class MongosConfiguration

Class for mongos configuration.


— database: database name. — username: username. — password: password. — hosts: full list of hosts to connect to, needed for the URI.

  • port: integer for the port to connect to connect to mongodb.
  • tls_external: indicator for use of internal TLS connection.
  • tls_internal: indicator for use of external TLS connection.


MongosConfiguration. uri( self )


Return URI concatenated from fields. None

class NotEnoughSpaceError


Raised when there isn't enough space to movePrimary. None

class ShardNotInClusterError


Raised when shard is not present in cluster, but it is expected to be. None

class ShardNotPlannedForRemovalError


Raised when it is expected that a shard is planned for removal, but it is not. None

class NotDrainedError


Raised when a shard is still being drained. None

class BalancerNotEnabledError


Raised when balancer process is not enabled. None

class MongosConnection

In this class we create connection object to Mongos.


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


MongosConnection. __init__( self , config: MongosConfiguration , uri , direct )

A MongoDB client interface.



MongoDB Configuration object.


allow using custom MongoDB URI, needed for replSet init.


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

MongosConnection. __enter__( self )


Return a reference to the new connection. None

MongosConnection. __exit__( self , object_type , value , traceback )


Disconnect from MongoDB client. None

MongosConnection. get_shard_members( self )

Gets shard members.


Returns: A set of the shard members as reported by mongos.

Raises: ConfigurationError, OperationFailure

MongosConnection. add_shard( self , shard_name , shard_hosts , shard_port )

Adds shard to the cluster.


Raises: ConfigurationError, OperationFailure

MongosConnection. pre_remove_checks( self , shard_name )

Performs a series of checks for removing a shard from the cluster.


Raises ConfigurationError, OperationFailure, NotReadyError, ShardNotInClusterError, BalencerNotEnabledError

MongosConnection. remove_shard( self , shard_name: str )

Removes shard from the cluster.


Raises: ConfigurationError, OperationFailure, NotReadyError, NotEnoughSpaceError, ShardNotInClusterError, BalencerNotEnabledError

MongosConnection. get_databases_for_shard( self , primary_shard )

Returns a list of databases using the given shard as a primary shard.


In Sharded MongoDB clusters, mongos selects the primary shard when creating a new database by picking the shard in the cluster that has the least amount of data. This means that:

  1. There can be multiple primary shards in a cluster.
  2. Until there is data written to the cluster there is effectively no primary shard.

MongosConnection. is_ready( self )

Is mongos 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.

Raises: ConfigurationError, ConfigurationError, OperationFailure

MongosConnection. is_shard_aware( self , shard_name: str )


Returns True if provided shard is shard aware. None

MongosConnection. get_db_size( self , database_name , primary_shard )


Returns the size of a DB on a given shard in bytes. None

MongosConnection. get_shard_with_most_available_space( self , shard_to_ignore )

Returns the shard in the cluster with the most available space and the space in bytes.


Algorithm used was similar to that used in mongo in selectShardForNewDatabase:

MongosConnection. get_draining_shards( self )


Returns a list of the shards currently draining. None