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.
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.
-
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.
-
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.
Import the mongodb
module into the Ballerina project.
import ballerinax/mongodb;
mongodb:Client mongoDb = check new ({
connection: {
serverAddress: {
host: "localhost",
port: 27017
},
auth: <mongodb:ScramSha256AuthCredential>{
username: <username>,
password: <password>,
database: <admin-database>
}
}
});
mongodb:Client mongoDb = check new ({
connection: <connection string obtained from the MongoDB server>
});
Now, you can use the available connector operations to interact with MongoDB server.
mongodb:Database moviesDb = check mongoDb->getDatabase("movies");
mongodb:Collection moviesCollection = check moviesDb->getCollection("movies");
Movie movie = {title: "Inception", year: 2010};
check moviesCollection->insert(movie);
Save the changes and run the Ballerina application using the following command.
bal run
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.
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.
-
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. -
Download and install Ballerina Swan Lake.
-
Download and install Docker.
Note: Ensure that the Docker daemon is running before executing any tests.
-
Export Github Personal access token with read package permissions as follows,
export packageUser=<Username> export packagePAT=<Personal access token>
Execute the commands below to build from the source.
-
To build the package:
./gradlew clean build
-
To run the tests:
./gradlew clean test
-
To build the without the tests:
./gradlew clean build -x test
-
To run selected test groups:
./gradlew clean test -P groups=<Comma separated groups/test cases>
-
To debug package with a remote debugger:
./gradlew clean build -P debug=<port>
-
To debug with the Ballerina language:
./gradlew clean build -P balJavaDebug=<port>
-
Publish the generated artifacts to the local Ballerina Central repository:
./gradlew clean build -P publishToLocalCentral=true
As an open-source project, Ballerina welcomes contributions from the community.
For more information, go to the contribution guidelines.
All the contributors are encouraged to read the Ballerina Code of Conduct.
- For more information go to the
mongodb
package. - For example demonstrations of the usage, go to Ballerina By Examples.
- Chat live with us via our Discord server.
- Post all technical questions on Stack Overflow with the #ballerina tag.