Guest User

Untitled

a guest
Nov 2nd, 2018
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import wso2/mongodb;
  2. import ballerina/io;
  3.  
  4. public function main() {
  5. // All 3 of the following endpoint declarations would work. You can try either of them. I'll leave two of them commented out.
  6. // Feel free to replace the configuration with yours and try these out.
  7.  
  8. endpoint mongodb:Client conn {
  9. dbName: "test",
  10. 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"}
  11. };
  12.  
  13. //endpoint mongodb:Client conn {
  14. // 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",
  15. // dbName: "test",
  16. // username: "test1",
  17. // password: "test1",
  18. // options: {authSource : "admin", sslEnabled: true, retryWrites: true, replicaSet: "Cluster0-shard-0"}
  19. //};
  20. //
  21. //endpoint mongodb:Client conn {
  22. // dbName: "test",
  23. // options: { url:"mongodb+srv://test1:test1@cluster0-gyqsg.mongodb.net/test?retryWrites=true" }
  24. //};
  25.  
  26. json doc1 = { "name": "Harry Potter", "author": "J K Rowling" };
  27. json doc2 = { "name": "Da Vinci Code", "author": "Dan Brown" };
  28. json doc3 = { "name": "Sherlock Holmes", "author": "Sir Arthur Connon Doyle" };
  29.  
  30. var ret = conn->insert("Books", doc1);
  31. handleInsert(ret, "Insert to Books");
  32. ret = conn->insert("Books", doc2);
  33. handleInsert(ret, "Insert to Books");
  34. ret = conn->insert("Books", doc3);
  35. handleInsert(ret, "Insert to Books");
  36.  
  37. var jsonRet = conn->find("Books", ());
  38. match jsonRet {
  39. json j => {
  40. io:print("initial data:");
  41. io:println(io:sprintf("%s", j));
  42. }
  43. error e => io:println("find failed: " + e.message);
  44. }
  45.  
  46. json queryString = { "name": "Harry Potter" };
  47. jsonRet = conn->find("Books", queryString);
  48. match jsonRet {
  49. json j => {
  50. io:print("query result:");
  51. io:println(io:sprintf("%s", j));
  52. }
  53. error e => io:println("find failed: " + e.message);
  54. }
  55.  
  56. jsonRet = conn->findOne("Books", queryString);
  57. match jsonRet {
  58. json j => {
  59. io:print("findOne query result:");
  60. io:println(io:sprintf("%s", j));
  61. }
  62. error e => io:println("find failed: " + e.message);
  63. }
  64.  
  65. json filter = { "name": "Sherlock Holmes" };
  66. var deleteRet = conn->delete("Books", filter, true);
  67. match deleteRet {
  68. int i => io:println("deleted count: " + i);
  69. error e => io:println("delete failed: " + e.message);
  70. }
  71.  
  72. conn.stop();
  73. }
  74.  
  75. function handleInsert(()|error returned, string message) {
  76. match returned {
  77. () => io:println(message + " success ");
  78. error e => io:println(message + " failed: " + e.message);
  79. }
  80. }
Add Comment
Please, Sign In to add comment