Advertisement
Guest User

Untitled

a guest
Apr 17th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. /*
  2. * A Java Script containing methods to manipulate a MongoDB
  3. * Biney Kingsley - 07-04-2019
  4. *
  5. * */
  6.  
  7. package com.mongo;
  8.  
  9. import org.bson.Document;
  10.  
  11. import com.mongodb.BasicDBObject;
  12. import com.mongodb.MongoClient;
  13. import com.mongodb.MongoCredential;
  14. import com.mongodb.client.FindIterable;
  15. import com.mongodb.client.MongoCollection;
  16. import com.mongodb.client.MongoDatabase;
  17. import com.mongodb.client.model.Filters;
  18.  
  19. import java.util.*;
  20.  
  21. public class MongoServices {
  22. // make some variables global for general access
  23. private MongoClient mongoClient;
  24. private MongoCredential credential;
  25. private MongoCollection<Document> collection;
  26. private MongoDatabase db;
  27.  
  28. // create a MongoClient and start server on a port with a host
  29. protected void connectServer(String host, int port) {
  30. /*
  31. * host: host of server
  32. * port: port to connect to
  33. * */
  34. try {
  35. mongoClient = new MongoClient(host, port);
  36. System.out.println("Connected successfully");
  37. }catch(Exception e) {
  38. System.out.print("Unable to connect to server");
  39. }
  40. }
  41.  
  42. // return the client server after creation
  43. protected MongoClient getClient() {
  44. return mongoClient;
  45. }
  46.  
  47. // create a database with user credentials
  48. protected void createCredentials(String username, String password, String dbName) {
  49. /*
  50. * username: database protection credentials
  51. * password: database protection credentials
  52. * dbName: name of database to be created
  53. * */
  54. try {
  55. credential = MongoCredential.createCredential(username, dbName, password.toCharArray());
  56. // print status
  57. System.out.println("Database Created successfully");
  58. }catch(Exception e) {
  59. System.out.println("Unable to create database");
  60. }
  61. }
  62.  
  63. // get the database credentials
  64. protected MongoCredential getCredential() {
  65. return credential;
  66. }
  67.  
  68. // access the created database
  69. protected void setDb(String dbName) {
  70. /*
  71. * dbName: access the database created and view credentials set to it
  72. * */
  73. try {
  74. mongoClient = getClient();
  75. db = mongoClient.getDatabase(dbName);
  76. System.out.println("Database Credentials::" + credential);
  77. }catch(Exception e) {
  78. System.out.println("Cannot access Database with name " + dbName);
  79. }
  80. }
  81.  
  82. // get the created database
  83. protected MongoDatabase getDb() {
  84. return db;
  85. }
  86.  
  87. // create a collection
  88. protected void createCollection(MongoDatabase db, String collectionName) {
  89. /*
  90. * db: Database to hold collection
  91. * collectionName: Name of the collection to be created
  92. * */
  93. try {
  94. db.createCollection(collectionName);
  95. System.out.println("Collection " + collectionName + " created successfully");
  96. }catch(Exception e) {
  97. System.out.println("Collection " + collectionName + " creation failed");
  98. }
  99. }
  100.  
  101. // select a collection to perform operations on
  102. protected MongoCollection<Document> getCollection(String collectionName){
  103. /*
  104. * collectionName: select a collection to perform operations on it
  105. * */
  106. MongoDatabase db = getDb();
  107. MongoCollection<Document> collection = db.getCollection(collectionName);
  108. System.out.println("Collection " + collectionName + " has been selected");
  109. return collection;
  110. }
  111.  
  112. // insert documents into a collection
  113. protected void insert(MongoCollection<Document> collection, Document doc) {
  114. /*
  115. * collection: the collection to insert documents in it
  116. * doc: the document to insert into the database
  117. * */
  118. try {
  119. collection.insertOne(doc);
  120. System.out.println("Document has been inserted into collection");
  121. }catch(Exception e) {
  122. System.out.println("Could not insert document into collection");
  123. }
  124. }
  125.  
  126. // view all documents in a collection
  127. protected void printDocs(MongoCollection<Document> collection) {
  128. /*
  129. * collection: collection to view all documents within
  130. * */
  131. try {
  132. FindIterable<Document> iterDoc = collection.find();
  133. Iterator it = iterDoc.iterator();
  134. while(it.hasNext()) {
  135. System.out.println(it.next());
  136. }
  137. }catch(Exception e) {
  138. System.out.println("Failed to print documents in the collection");
  139. }
  140. }
  141.  
  142. // update a document
  143. protected void updateDocument(MongoCollection<Document> collection, String key, String value, BasicDBObject newDocument) {
  144. /*
  145. * collection: the collection containing the document to update
  146. * key: the unique key (ideally id field) of the document to update
  147. * value: the value of the key (ideally the id value) of the document to update
  148. * newDocument: data to update the current data in the document with
  149. * */
  150. try {
  151. BasicDBObject searchquery = new BasicDBObject().append(key, value);
  152. collection.updateOne(searchquery, newDocument);
  153. System.out.println("Updated the Document successfully");
  154. }catch(Exception e) {
  155. System.out.println("Could not update the document with " + key + ":" + value + " pairs");
  156. }
  157. }
  158.  
  159. // delete a document
  160. protected void deleteDocument(MongoCollection<Document> collection, String key, String value) {
  161. /*
  162. * collection: the collection containing the document to delete
  163. * key: the unique key field of the document to delete
  164. * value: the value of the unique key field of the document to delete
  165. * */
  166. try {
  167. collection.deleteOne(Filters.eq(key, value));
  168. System.out.print("Document with field " + key + ":" + value + " has been deleted");
  169. }catch(Exception e) {
  170. System.out.println("Document deletion failed");
  171. }
  172. }
  173.  
  174. // list collections in a db
  175. protected void listCollection(MongoDatabase db) {
  176. /*
  177. * db: database in focus
  178. * */
  179. try {
  180. for (String collNames: db.listCollectionNames()) {
  181. System.out.println(collNames);
  182. }
  183. }catch(Exception e) {
  184. System.out.println("Could not list collections");
  185. }
  186. }
  187.  
  188. // drop a collection in a db
  189. protected void dropCollection(MongoDatabase db, String collectionName) {
  190. /*
  191. * db: database in focus
  192. * collectionName: collection to drop
  193. * */
  194. try {
  195. MongoCollection<Document> collection = db.getCollection(collectionName);
  196. collection.drop();
  197. System.out.println("Collection " + collectionName + " dropped successfully");
  198. }catch(Exception e) {
  199. System.out.println("Could not drop collection " + collectionName);
  200. }
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement