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

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

## charms.mongodb.v1.helpers

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

    Fetch library

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

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

Simple functions, which can be used in both K8s and VM charms.

---

Index

* [def get\_create\_user\_cmd(
  config,
  mongo\_path
  )](https://charmhub.io/mongodb/libraries/helpers#get_create_user_cmd)
* [def get\_mongos\_args(
  config,
  snap\_install,
  config\_server\_db,
  external\_connectivity
  )](https://charmhub.io/mongodb/libraries/helpers#get_mongos_args)
* [def get\_mongod\_args(
  config,
  auth,
  snap\_install,
  role
  )](https://charmhub.io/mongodb/libraries/helpers#get_mongod_args)
* [def generate\_password(
  )](https://charmhub.io/mongodb/libraries/helpers#generate_password)
* [def generate\_keyfile(
  )](https://charmhub.io/mongodb/libraries/helpers#generate_keyfile)
* [def copy\_licenses\_to\_unit(
  )](https://charmhub.io/mongodb/libraries/helpers#copy_licenses_to_unit)
* [def current\_pbm\_op(
  pbm\_status
  )](https://charmhub.io/mongodb/libraries/helpers#current_pbm_op)
* [def process\_pbm\_status(
  pbm\_status
  )](https://charmhub.io/mongodb/libraries/helpers#process_pbm_status)
* [def add\_args\_to\_env(
  var,
  args
  )](https://charmhub.io/mongodb/libraries/helpers#add_args_to_env)
* [def safe\_exec(
  command,
  env,
  working\_dir
  )](https://charmhub.io/mongodb/libraries/helpers#safe_exec)

#### def get\_create\_user\_cmd(     config: MongoConfiguration,     mongo\_path )

Creates initial admin user for MongoDB.

Description

Initial admin user can be created only through localhost connection.
see https://www.mongodb.com/docs/manual/core/localhost-exception/
unfortunately, pymongo not able to create connection which considered
as local connection by MongoDB, even if socket connection used.
As result where are only hackish ways to create initial user.
It is needed to install mongodb-clients inside charm container to make
this function work correctly

#### def get\_mongos\_args(     config,     snap\_install: bool,     config\_server\_db: str,     external\_connectivity: bool )

Returns the arguments used for starting mongos on a config-server side application.

Returns

A string representing the arguments to be passed to mongos.

#### def get\_mongod\_args(     config: MongoConfiguration,     auth: bool,     snap\_install: bool,     role: str )

Construct the MongoDB startup command line.

Returns

A string representing the command used to start MongoDB.

#### def generate\_password()

Generate a random password string.

Returns

A random password string.

#### def generate\_keyfile()

Key file used for authentication between replica set peers.

Returns

A maximum allowed random string.

#### def copy\_licenses\_to\_unit()

Description

Copies licenses packaged in the snap to the charm's licenses directory. None

#### def current\_pbm\_op(pbm\_status: str)

Description

Parses pbm status for the operation that pbm is running. None

#### def process\_pbm\_status(pbm\_status: str)

Description

Parses current pbm operation and returns unit status. None

#### def add\_args\_to\_env(     var: str,     args: str )

Description

Adds the provided arguments to the environment as the provided variable. None

#### def safe\_exec(     command,     env,     working\_dir )

Description

Execs a command on the workload in a safe way. None
