ballerinax/mongodb Ballerina library

5.2.4

Overview

MongoDB is a document-oriented, NoSQL database designed for high volume data storage. It offers a flexible schema, high availability, and horizontal scalability, making it ideal for modern application development.

The MongoDB connector offers APIs to connect to MongoDB servers and perform various operations including CRUD operations, indexing, and aggregation.

Key Features

  • Support for MongoDB v3.6 and later versions
  • Comprehensive CRUD operations for collections and documents
  • Support for indexing and aggregation pipelines
  • Connection management via connection string or parameters
  • GraalVM compatible for native image builds

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.

Copy
import ballerinax/mongodb;

Step 2: Initialize the MongoDB client

Initialize the MongoDB client using the connection parameters

Copy
mongodb:Client mongoDb = check 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

Copy
mongodb:Client mongoDb = check new ({
    connection: <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

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

Retrieve a Collection

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

Insert a Document

Copy
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.

Copy
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.

Import

import ballerinax/mongodb;Copy

Other versions

See more...

Metadata

Released date: about 17 hours ago

Version: 5.2.4

License: Apache-2.0


Compatibility

Platform: java21

Ballerina version: 2201.12.0

GraalVM compatible: Yes


Pull count

Total: 13225

Current verison: 6


Weekly downloads


Source repository


Keywords

IT Operations/Database

Cost/Freemium

NoSQL

Vendor/MongoDB

Area/Database

Type/Connector


Contributors