ballerinax/mongodb Ballerina library



MongoDB is a document database designed to cater to modern application requirements. It is a scalable and a flexible solution that inherently supports distributed system design. Ballerina MongoDB Connector allows you to perform the MongoDB CRUD operations.

This module supports MongoDB version 4.2.


Before using this connector in your Ballerina application, complete the following:

  1. Make sure a MongoDB is available to connect.

  2. Obtain connection details such as connection URL or hostname, port number, username, and password to connect the database.


To use the MongoDB connector in your Ballerina application, update the .bal file as follows:

Step 1: Import connector

Import the ballerinax/mongodb module into the Ballerina project.

import ballerinax/mongodb;

Step 2: Create a new connector instance

Create a mongodb:ConnectionConfig with connection details obtained, and initialize the connector with it.

To use the MongoDB client you need to specify the database it needs to connect to. If you plan to use this client to connect to single database then you can pass the database name along with the other configurations required for client initialization(optional). Alternatively, you can pass the database name for each remote method call. This is not recommended unless you need to connect to more than one database using the client.

mongodb:ConnectionConfig mongoConfig = {
    connection: {
        host: <YOUR_HOST_NAME>,
        port: <PORT>,
        auth: {
            username: <DB_USERNAME>,
            password: <DB_PASSWORD>
        options: {
            sslEnabled: false, 
            serverSelectionTimeout: 5000
    databaseName: DATABASE_NAME>
mongodb:Client mongoClient = check new (mongoConfig);

Step 3: Invoke connector operation

  1. Now you can use the operations available within the connector. Note that they are in the form of remote operations.
    Following is an example on how to insert a document into a collection using the connector.
    public function main() returns error? {
        string collection = "<COLLECTION_NAME>"
        map<json> doc = { "name": "Gmail", "version": "0.99.1", "type" : "Service" };
        check mongoClient->insert(doc, collection);
  2. Use bal run command to compile and run the Ballerina program.


import ballerinax/mongodb;Copy


Released date: 9 months ago

Version: 4.3.0

License: Apache-2.0


Platform: java11

Ballerina version: 2201.7.0

GraalVM compatible: Yes

Pull count

Total: 0

Current verison: 576

Weekly downloads

Source repository


IT Operations/Databases



Other versions

See more...