Module mongodb

ballerinax/mongodb Ballerina library
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
-
Download and install the MongoDB server from the MongoDB download center.
-
Follow the installation instructions provided in the download center.
-
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
-
Sign up for a free account in MongoDB Atlas.
-
Follow the instructions provided in the MongoDB Atlas documentation to create a new cluster.
-
Navigate to your MongoDB Atlas cluster.
-
Select "Database" from the left navigation pane under the "Deployment" section and click "connect" button to open connection instructions.
-
Add your IP address to the IP access list or select "Allow access from anywhere" to allow all IP addresses to access the cluster.
-
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.
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.
- Movie database - Implement a movie database using MongoDB.
- Order management system - Implement an order management system using MongoDB.