ballerinax/mongodb Ballerina library

5.0.0

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.

Copy
import ballerinax/mongodb;

Step 2: Initialize the MongoDB client

Initialize the MongoDB client using the connection parameters

Copy
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

Copy
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

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

Metadata

Released date: 8 months ago

Version: 5.0.0

License: Apache-2.0


Compatibility

Platform: java17

Ballerina version: 2201.8.6

GraalVM compatible: Yes


Pull count

Total: 6371

Current verison: 745


Weekly downloads


Source repository


Keywords

IT Operations/Databases

Cost/Freemium


Contributors

Other versions

See more...