Skip to content

ballerina-platform/module-ballerinax-mongodb

Repository files navigation

Ballerina MongoDB Connector

Build Status Trivy codecov GitHub Last Commit GraalVM Check License

Overview

MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era. MongoDB offers both a Community and an Enterprise version of the database.

The ballerinax/mongodb package offers APIs to connect to MongoDB servers and to perform various operations including CRUD operations, indexing, and aggregation.

The Ballerina MongoDB connector is compatible with MongoDB 3.6 and later versions.

Setup guide

To use the MongoDB connector, you need to have a MongoDB server running and accessible. For that, you can either install MongoDB locally or use the MongoDB Atlas, the cloud offering of the MongoDB.

Setup a MongoDB server locally

  1. Download and install the MongoDB server from the MongoDB download center.

  2. Follow the installation instructions provided in the download center.

  3. Follow the instructions for each operating system to start the MongoDB server.

Note: This guide uses the MongoDB community edition for the setup. Alternatively, the enterprise edition can also be used.

Setup a MongoDB server using MongoDB Atlas

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

  2. Follow the instructions provided in the MongoDB 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 click "connect" button to open connection instructions.

    MongoDB Atlas Connect

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

    MongoDB Atlas IP Access List

  6. Click "Choose a connection method" and select "Drivers" under the "Connect your application". There you can find the connection string to connect to the MongoDB Atlas cluster.

    MongoDB Atlas Connection Method

Quickstart

Step 1: Import the module

Import the mongodb module into the Ballerina project.

import ballerinax/mongodb;

Step 2: Initialize the MongoDB client

Initialize the MongoDB client using the connection parameters

mongodb:Client mongoDb = new ({
    connection: {
        serverAddress: {
            host: "localhost",
            port: 27017
        },
        auth: <mongodb:ScramSha256AuthCredential>{
            username: <username>,
            password: <password>,
            database: <admin-database>
        }
    }
});

Initialize the MongoDB client using the connection string

mongodb:Client mongoDb = new ({
    connectionString: <connection string obtained from the MongoDB server>
});

Step 3: Invoke the connector operation

Now, you can use the available connector operations to interact with MongoDB server.

Retrieve a Database

mongodb:Database moviesDb = check mongoDb->getDatabase("movies");

Retrieve a Collection

mongodb:Collection moviesCollection = check moviesDb->getCollection("movies");

Insert a Document

Movie movie = {title: "Inception", year: 2010};
check moviesCollection->insert(movie);

Step 4: Run the Ballerina application

Save the changes and run the Ballerina application using the following command.

bal run

Examples

The MongoDB connector provides practical examples illustrating usage in various scenarios. Explore these examples covering common MongoDB operations.

  1. Movie database - Implement a movie database using MongoDB.
  2. Order management system - Implement an order management system using MongoDB.

Issues and projects

The Issues and Projects tabs are disabled for this repository as this is part of the Ballerina library. To report bugs, request new features, start new discussions, view project boards, etc., visit the Ballerina library parent repository.

Building from the source

Setting up the prerequisites

  1. Download and install Java SE Development Kit (JDK) version 17. You can download it from either of the following sources:

    Note: After installation, remember to set the JAVA_HOME environment variable to the directory where JDK was installed.

  2. Download and install Ballerina Swan Lake.

  3. Download and install Docker.

    Note: Ensure that the Docker daemon is running before executing any tests.

  4. Export Github Personal access token with read package permissions as follows,

    export packageUser=<Username>
    export packagePAT=<Personal access token>

Building the source

Execute the commands below to build from the source.

  1. To build the package:

        ./gradlew clean build
  2. To run the tests:

    ./gradlew clean test
  3. To build the without the tests:

    ./gradlew clean build -x test
  4. To run selected test groups:

    ./gradlew clean test -P groups=<Comma separated groups/test cases>
  5. To debug package with a remote debugger:

    ./gradlew clean build -P debug=<port>
  6. To debug with the Ballerina language:

    ./gradlew clean build -P balJavaDebug=<port>
  7. Publish the generated artifacts to the local Ballerina Central repository:

    ./gradlew clean build -P publishToLocalCentral=true

Contribute to Ballerina

As an open-source project, Ballerina welcomes contributions from the community.

For more information, go to the contribution guidelines.

Code of conduct

All the contributors are encouraged to read the Ballerina Code of Conduct.

Useful links