Skip to main content

Setup Guide

This guide walks you through setting up a MongoDB instance and obtaining the connection string required to use the MongoDB connector.

Prerequisites

  • A MongoDB instance (v3.6 or later). You can use MongoDB Atlas for a cloud-hosted instance or install MongoDB Community Edition locally.

Step 1: Set up a MongoDB server locally

  1. Download and install MongoDB Community Edition from the MongoDB download center.
  2. Follow the installation instructions for your operating system to start the MongoDB server.
note

This guide uses MongoDB Community Edition. The Enterprise Edition can also be used.

Step 2: Set up a MongoDB Atlas cluster

If you prefer a cloud-hosted instance:

  1. Sign up for a free account at MongoDB Atlas.

  2. Follow the Atlas documentation to create a new cluster.

  3. Navigate to your MongoDB Atlas cluster.

  4. Select Database from the left navigation pane under the Deployment section and select Connect to open the connection instructions.

    MongoDB Atlas database page

  5. Add your IP address to the IP access list, or select Allow access from anywhere to allow all IP addresses.

    MongoDB Atlas IP access list

  6. Select Choose a connection method and select Drivers under Connect your application to find the connection string.

    MongoDB Atlas connection method

warning

Allowing access from all IP addresses (0.0.0.0/0) is suitable for development only. In production, restrict access to specific IP addresses or CIDR ranges.

Step 3: Get the connection string

For MongoDB Atlas, copy the connection string from the Drivers connection method page. It follows this format:

mongodb+srv://<username>:<password>@<cluster-host>/?retryWrites=true&w=majority

Replace <username> and <password> with your database user credentials.

For local MongoDB, the default connection string is:

mongodb://localhost:27017

If authentication is enabled:

mongodb://myUser:myPassword@localhost:27017/?authSource=admin
tip

Store the connection string securely. Use Ballerina's configurable feature and a Config.toml file to supply it at runtime.