IBM Cloud Docs
Connecting with the MongoDB Shell

Connecting with the MongoDB Shell

You can access your MongoDB database from a command-line client, which allows for direct interaction and monitoring of the data structures that are created within the database. Use it to query and update data, as well to perform administrative operations and monitor performance.

Installing

The MongoDB shell is available as part of the MongoDB distribution. Download it here.

Connecting

Connection strings are displayed in the Endpoints panel of your deployment's Overview, and can also be retrieved from the cloud databases CLI plugin, and the API.

The information the MongoDB shell needs to connect to your instance is in the "cli" section of the connection strings. The table contains a breakdown for reference.

Table 1. mongo/cli connection information
Field Name Index Description
Bin The recommended binary to create a connection; in this case it is mongo.
Composed A formatted command to establish a connection to your deployment. The command combines the Bin executable, Environment variable settings, and uses
Environment A list of key/values you set as environment variables.
Arguments 0... The information that is passed as arguments to the command shown in the Bin field.
Certificate Base64 A self-signed certificate that is used to confirm that an application is connecting to the appropriate server. It is base64 encoded.
Certificate Name The allocated name for the self-signed certificate.
Type The type of package that uses this connection information; in this case cli.
  • 0... indicates that there might be one or more of these entries in an array.

MongoDB Shell Example

mongosh -u admin -p $PASSWORD --ssl --sslCAFile c5f07836-d94c-11e8-a2e9-62ec2ed68f84 --authenticationDatabase admin --host replset/bd574ce4-7b36-4274-9976-96db98a3ac10-0.b8a5e798d2d04f2e860e54e5d042c915.databases.appdomain.cloud:30484,bd574ce4-7b36-4274-9976-96db98a3ac10-1.b8a5e798d2d04f2e860e54e5d042c915.databases.appdomain.cloud:30484
  • mongosh - The command itself.
  • --ssl --sslCAFile - The path and name of the self-signed certificate for your deployment.
  • -u - The parameter for the username.
  • -p - The parameter for the password.
  • --authenticationDatabase - The database where the user and its credentials are created and stored.
  • --host - The replica set name, followed by a /, and the hosts of the replica set members.

Starting the MongoDB Shell from the IBM Cloud CLI

The ibmcloud cdb deployment-connections command handles everything that is involved in creating the client connection. For example, to connect to a deployment named "example-mongo" with an "example-user", use the following command.

ibmcloud cdb deployment-connections --start -u example-user example-mongo

The command prompts for the user's password and then runs the MongoDB command-line client to connect to the database.

The option --start must come before the parameters, otherwise connection information is returned and the MongoDB Shell is not started.

Using the self-signed certificate

  1. Copy the certificate information from the Endpoints panel or the Base64 field of the connection information.
  2. If needed, decode the Base64 string into text.
  3. Save the certificate to a file. (You can use the Name that is provided or your own file name).
  4. Provide the path to the certificate to the --sslCAFile parameter.

You can display the decoded certificate for your deployment with the CLI plug-in with the command ibmcloud cdb deployment-cacert "your-service-name". It decodes the base64 into text. Copy and save the command's output to a file and provide the file's path to the --sslCAFile parameter.