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

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

## charms.mongodb.v1.mongos

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

    Fetch library

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

    [Download mongos.py](https://charmhub.io/mongodb/libraries/mongos/download)
  + *Last updated* 21 Aug 2024
  + *Revision* Library version 1.7

Code for interactions with MongoDB.

---

Index

* [class NotEnoughSpaceError](https://charmhub.io/mongodb/libraries/mongos#notenoughspaceerror)
* [class ShardNotInClusterError](https://charmhub.io/mongodb/libraries/mongos#shardnotinclustererror)
* [class ShardNotPlannedForRemovalError](https://charmhub.io/mongodb/libraries/mongos#shardnotplannedforremovalerror)
* [class NotDrainedError](https://charmhub.io/mongodb/libraries/mongos#notdrainederror)
* [class BalancerNotEnabledError](https://charmhub.io/mongodb/libraries/mongos#balancernotenablederror)
* [class MongosConnection](https://charmhub.io/mongodb/libraries/mongos#mongosconnection)
* + [def \_\_init\_\_(
    self,
    config,
    uri,
    direct)](https://charmhub.io/mongodb/libraries/mongos#mongosconnection-__init__)
* + [def get\_shard\_members(
    self)](https://charmhub.io/mongodb/libraries/mongos#mongosconnection-get_shard_members)
* + [def add\_shard(
    self,
    shard\_name,
    shard\_hosts,
    shard\_port)](https://charmhub.io/mongodb/libraries/mongos#mongosconnection-add_shard)
* + [def pre\_remove\_checks(
    self,
    shard\_name)](https://charmhub.io/mongodb/libraries/mongos#mongosconnection-pre_remove_checks)
* + [def start\_and\_wait\_for\_balancer(
    self)](https://charmhub.io/mongodb/libraries/mongos#mongosconnection-start_and_wait_for_balancer)
* + [def remove\_shard(
    self,
    shard\_name)](https://charmhub.io/mongodb/libraries/mongos#mongosconnection-remove_shard)
* + [def get\_databases\_for\_shard(
    self,
    primary\_shard)](https://charmhub.io/mongodb/libraries/mongos#mongosconnection-get_databases_for_shard)
* + [def is\_any\_draining(
    self,
    ignore\_shard)](https://charmhub.io/mongodb/libraries/mongos#mongosconnection-is_any_draining)
* + [def is\_ready(
    self)](https://charmhub.io/mongodb/libraries/mongos#mongosconnection-is_ready)
* + [def are\_all\_shards\_aware(
    self)](https://charmhub.io/mongodb/libraries/mongos#mongosconnection-are_all_shards_aware)
* + [def is\_shard\_aware(
    self,
    shard\_name)](https://charmhub.io/mongodb/libraries/mongos#mongosconnection-is_shard_aware)
* + [def get\_db\_size(
    self,
    database\_name,
    primary\_shard)](https://charmhub.io/mongodb/libraries/mongos#mongosconnection-get_db_size)
* + [def get\_shard\_with\_most\_available\_space(
    self,
    shard\_to\_ignore)](https://charmhub.io/mongodb/libraries/mongos#mongosconnection-get_shard_with_most_available_space)
* + [def get\_draining\_shards(
    self)](https://charmhub.io/mongodb/libraries/mongos#mongosconnection-get_draining_shards)

#### class NotEnoughSpaceError

Description

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

#### class ShardNotInClusterError

Description

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

#### class ShardNotPlannedForRemovalError

Description

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

#### class NotDrainedError

Description

Raised when a shard is still being drained. None

#### class BalancerNotEnabledError

Description

Raised when balancer process is not enabled. None

#### class MongosConnection

In this class we create connection object to Mongos.

Description

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>

Methods

MongosConnection.
\_\_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.

MongosConnection.
get\_shard\_members(

*self*
)

Gets shard members.

Description

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.

Description

Raises:
ConfigurationError, OperationFailure

MongosConnection.
pre\_remove\_checks(

*self*

,
shard\_name
)

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

Description

Raises
ConfigurationError, OperationFailure, NotReadyError, ShardNotInClusterError,
BalencerNotEnabledError

MongosConnection.
start\_and\_wait\_for\_balancer(

*self*
)

Turns on the balancer and waits for it to be running.

Description

Starting the balancer doesn't guarantee that is is running, wait until it starts up.

Raises:
BalancerNotEnabledError

MongosConnection.
remove\_shard(

*self*

,
shard\_name: str
)

Removes shard from the cluster.

Description

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.

Description

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\_any\_draining(

*self*

,
ignore\_shard: str
)

Returns true if any shard members is draining.

Arguments

sc\_status

current state of shard cluster status as reported by mongos.

ignore\_shard

shard to ignore

Description

Checks if any members in sharded cluster are draining data.

MongosConnection.
is\_ready(

*self*
)

Is mongos ready for services requests.

Description

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.
are\_all\_shards\_aware(

*self*
)

Description

Returns True if all shards are shard aware. None

MongosConnection.
is\_shard\_aware(

*self*

,
shard\_name: str
)

Description

Returns True if provided shard is shard aware. None

MongosConnection.
get\_db\_size(

*self*

,
database\_name

,
primary\_shard
)

Description

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.

Description

Algorithm used was similar to that used in mongo in `selectShardForNewDatabase`:
https://github.com/mongodb/mongo/blob/6/0/src/mongo/db/s/config/sharding\_catalog\_manager\_database\_operations.cpp#L68-L91

MongosConnection.
get\_draining\_shards(

*self*
)

Description

Returns a list of the shards currently draining. None
