Ballerina MongoDB Client is used to connect Ballerina with MongoDB data source.
With the Ballerina MongoDB client following operations are supported.
- insert - To insert document to a given collection
- find - To select document from a given collection according to given query.
- findOne - To select the first document match with the query.
- update - To update documents that matches to the given filter.
- delete - To delete documents that matches to the given filter.
Steps to Configure
Samples
Performing CRUD operations with MongoDB client
Following is a simple Ballerina program that can be used to perform CRUD operations.
import ballerina/io;
import wso2/mongodb;
import ballerina/log;
mongodb:ClientEndpointConfig mongoConfig = {
host: "localhost",
dbName: "projectsTest1",
username: "",
password: "",
options: {sslEnabled: false, serverSelectionTimeout: 500}
};
public function main() returns error? {
mongodb:Client mongoClient = check new (mongoConfig);
json doc1 = { "name": "ballerina", "type": "src" };
json doc2 = { "name": "connectors", "type": "artifacts" };
json doc3 = { "name": "docerina", "type": "src" };
json doc4 = { "name": "test", "type": "artifacts" };
log:printInfo("------------------ Inserting Data -------------------");
var ret = mongoClient->insert("projects", doc1);
handleInsert(ret, "Insert to projects");
ret = mongoClient->insert("projects", doc2);
handleInsert(ret, "Insert to projects");
ret = mongoClient->insert("projects", doc3);
handleInsert(ret, "Insert to projects");
log:printInfo("------------------ Querying Data -------------------");
var jsonRet = mongoClient->find("projects", ());
handleFind(jsonRet);
json queryString = {name: "connectors" };
jsonRet = mongoClient->find("projects", queryString);
handleFind(jsonRet);
json jsonRetOne = mongoClient->findOne("projects", queryString);
handleFind(jsonRetOne);
log:printInfo("------------------ Updating Data -------------------");
json replaceFilter = { "type": "artifacts" };
json doc5 = { "name": "main", "type": "artifacts" };
boolean upsert = true;
int response = mongoClient->replace("projects", replaceFilter, doc5,upsert);
if (response > 0 ) {
log:printInfo("Modified count: ") ;
log:printInfo(response.toString());
} else {
log:printInfo("Error in replacing data");
}
log:printInfo("------------------ Deleting Data -------------------");
json deleteFilter = { "name": "ballerina" };
var deleteRet = mongoClient->delete("projects", deleteFilter, true);
if (response > 0 ) {
log:printInfo("Modified count: ") ;
log:printInfo(response.toString());
} else {
log:printInfo("Error in replacing data");
}
mongoClient.stop();
}
function handleInsert(json|error returned, string message) {
if (returned is error) {
log:printInfo(message + " failed: " , returned.reason());
} else {
log:printInfo(message + " success ");
}
}
function handleFind(json|error returned) {
if (returned is json) {
log:printInfo("initial data:");
log:printInfo(io:sprintf("%s", returned));
} else {
log:printInfo("find failed: " + returned.reason());
}
}```