IBM Cloud Docs
Getting started with Databases for MongoDB

Getting started with Databases for MongoDB

This tutorial guides you through the steps to quickly start using Databases for MongoDB by provisioning an instance, setting your Admin password, connecting to it and writing and reading a simple document.

Follow these steps to complete the tutorial:

Follow these steps to complete the tutorial:

Follow these steps to complete the tutorial:

Follow these steps to complete the tutorial:

Before you begin

Step 1: Choose your plan

Databases for MongoDB offers two different plans:

Using APIs

Use the Cloud Databases API to work with your Databases for MongoDB instance. The resource controller API is used to provision an instance.

You will need an API key to perform actions via the API. Follow these steps to create an IBM Cloud API key that enables you to use the API to provision infrastructure into your account. You can create up to 20 API keys.

For security reasons, the API key is only available to be copied or downloaded at the time of creation. If the API key is lost, you must create a new API key.

Step 2: Provision through the console

  1. Log in to the IBM Cloud console.

  2. Click the Databases for MongoDB service in the catalog.

  3. Follow these steps to provision a Databases for MongoDB instance.

  4. When your instance is provisioned, click the instance name to view more information.

Step 2: Provision through the CLI

You can provision a Databases for MongoDB instance by using the CLI. If you don't already have it, you need to install the IBM Cloud CLI.

You can follow these steps to provision a Databases for MongoDB instance.

Step 2: Provision through the resource controller API

Follow these steps to provision a Databases for MongoDB instance using the Resource Controller API.

Step 2: Provision through Terraform

You need an API key to perform actions via Terraform. Follow these steps to create an IBM Cloud API key that enables Terraform to provision infrastructure into your account. You can create up to 20 API keys.

For security reasons, the API key is only available to be copied or downloaded at the time of creation. If the API key is lost, you must create a new API key.

Once you have an API Key, follow these steps to provision a Databases for MongoDB instance using Terraform.

Step 3: Set the admin password

The admin user

When you provision a Databases for MongoDB deployment, an admin user is automatically created.

Set the admin password before using it to connect.

Set the admin password through the UI

Set your admin password through the UI by selecting your instance from the IBM Cloud Resource list. Then, select Settings. Next, select Change Database Admin password.

Set the admin password through the CLI

Use the cdb user-password command from the IBM Cloud CLI Cloud Databases plug-in to set the admin password.

For example, to set the admin password for your deployment, use the following command:

ibmcloud cdb user-password <INSTANCE_NAME_OR_CRN> admin <NEWPASSWORD>

Set the admin password through the API

YOu can use the id parameter obtained in the response to Step 2 above with the Set specified user's password endpoint to set the admin password.

curl -X PATCH -H "Authorization: Bearer <TOKEN>" \
     -H 'Content-Type: application/json' \
     -d '{"password":"newrootpasswordsupersecure21"}' \
      "https://api.<REGION>.databases.cloud.ibm.com/v5/ibm/deployments/<DEPLOYMENT_ID>/users/database/admin"

The id parameter needs to be URL-encoded for the above API call to work.

Setting the admin password through Terraform

The admin password is passed in as one of the database resource parameters in the Terraform script. There is no need for any further action.

Step 4: Connect to your Databases for MongoDB instance

You can easily connect to your instance by either using the Mongo Shell (a command line interface) or Mongo Compass, a powerful GUI for querying and analyizing your data. Both of these tools are provided my MongoDB.

Using the Mongo Shell

Follow these instructions to download and connect to the Mongo Shell.

You can then test your deployment by inserting a document into a collection:

use sample_mflix

db.movies.insertOne(
  {
    title: "The Favourite",
    genres: [ "Drama", "History" ],
    runtime: 121,
    rated: "R",
    year: 2018,
    directors: [ "Yorgos Lanthimos" ],
    cast: [ "Olivia Colman", "Emma Stone", "Rachel Weisz" ],
    type: "movie"
  }
)

The above command switches to a database called sample_mflix (and creates it if it does not already exist), and then insert a document into the movies collection (which gets also be created if it does not already exist).

You can then retrieve the document with:

db.movies.find( { title: "The Favourite" } )

You now connected to your database and wrote and read data using the Mongo Shell.

Using MongoDB Compass

Follow these instructions to download MongoDB Compass and use it to connect to your Databases for MongoDB instance. You can write and read data using the MongoDB Compass documentation.

Next steps