Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import wso2/mongodb;
- import ballerina/io;
- public function main() {
- // All 3 of the following endpoint declarations would work. You can try either of them. I'll leave two of them commented out.
- // Feel free to replace the configuration with yours and try these out.
- endpoint mongodb:Client conn {
- dbName: "test",
- options : { url: "mongodb://test1:test1@cluster0-shard-00-00-gyqsg.mongodb.net:27017,cluster0-shard-00-01-gyqsg.mongodb.net:27017,cluster0-shard-00-02-gyqsg.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true"}
- };
- //endpoint mongodb:Client conn {
- // host: "cluster0-shard-00-00-gyqsg.mongodb.net:27017,cluster0-shard-00-01-gyqsg.mongodb.net:27017,cluster0-shard-00-02-gyqsg.mongodb.net:27017",
- // dbName: "test",
- // username: "test1",
- // password: "test1",
- // options: {authSource : "admin", sslEnabled: true, retryWrites: true, replicaSet: "Cluster0-shard-0"}
- //};
- //
- //endpoint mongodb:Client conn {
- // dbName: "test",
- // options: { url:"mongodb+srv://test1:test1@cluster0-gyqsg.mongodb.net/test?retryWrites=true" }
- //};
- json doc1 = { "name": "Harry Potter", "author": "J K Rowling" };
- json doc2 = { "name": "Da Vinci Code", "author": "Dan Brown" };
- json doc3 = { "name": "Sherlock Holmes", "author": "Sir Arthur Connon Doyle" };
- var ret = conn->insert("Books", doc1);
- handleInsert(ret, "Insert to Books");
- ret = conn->insert("Books", doc2);
- handleInsert(ret, "Insert to Books");
- ret = conn->insert("Books", doc3);
- handleInsert(ret, "Insert to Books");
- var jsonRet = conn->find("Books", ());
- match jsonRet {
- json j => {
- io:print("initial data:");
- io:println(io:sprintf("%s", j));
- }
- error e => io:println("find failed: " + e.message);
- }
- json queryString = { "name": "Harry Potter" };
- jsonRet = conn->find("Books", queryString);
- match jsonRet {
- json j => {
- io:print("query result:");
- io:println(io:sprintf("%s", j));
- }
- error e => io:println("find failed: " + e.message);
- }
- jsonRet = conn->findOne("Books", queryString);
- match jsonRet {
- json j => {
- io:print("findOne query result:");
- io:println(io:sprintf("%s", j));
- }
- error e => io:println("find failed: " + e.message);
- }
- json filter = { "name": "Sherlock Holmes" };
- var deleteRet = conn->delete("Books", filter, true);
- match deleteRet {
- int i => io:println("deleted count: " + i);
- error e => io:println("delete failed: " + e.message);
- }
- conn.stop();
- }
- function handleInsert(()|error returned, string message) {
- match returned {
- () => io:println(message + " success ");
- error e => io:println(message + " failed: " + e.message);
- }
- }
Add Comment
Please, Sign In to add comment