Advertisement
Guest User

MongoDBHelper

a guest
Jan 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. import org.bson.Document;
  5. import org.bson.conversions.Bson;
  6.  
  7. import com.mongodb.BasicDBObject;
  8. import com.mongodb.MongoClient;
  9. import com.mongodb.client.MongoCollection;
  10. import com.mongodb.client.MongoDatabase;
  11.  
  12. public class MongoDBHelper {
  13.  
  14.     public MongoClient connection;
  15.     public MongoDatabase database;
  16.     public MongoCollection<Document> collection;
  17.  
  18.     public MongoDBHelper(String host, int port, String database, String collection) {
  19.         this.setConnection(host, port);
  20.         this.setDatabase(database);
  21.         this.setCollection(collection);
  22.     }
  23.    
  24.     public MongoClient getConnection() {
  25.         return connection;
  26.     }
  27.  
  28.     public void setConnection(MongoClient connection) {
  29.         this.connection = connection;
  30.     }
  31.    
  32.     public void setConnection(String host, int port) {
  33.         this.connection = new MongoClient(host, port);
  34.     }
  35.  
  36.     public MongoDatabase getDatabase() {
  37.         return database;
  38.     }
  39.    
  40.     public void setDatabase(MongoDatabase database) {
  41.         this.database = database;
  42.     }
  43.    
  44.     public void setDatabase(String database) {
  45.         this.database = this.getConnection().getDatabase(database);
  46.     }
  47.  
  48.     public MongoCollection<Document> getCollection() {
  49.         return collection;
  50.     }
  51.  
  52.     public void setCollection(MongoCollection<Document> collection) {
  53.         this.collection = collection;
  54.     }
  55.    
  56.     public void setCollection(String collection) {
  57.         this.collection = this.getDatabase().getCollection(collection);
  58.     }
  59.    
  60.     private List<Document> executeSelect(Bson query) {
  61.         List<Document> records = (List<Document>) this.getCollection().find(query).into(new ArrayList<Document>());
  62.         return records;
  63.     }
  64.    
  65.     public List<Document> getFieldValueEqualTo(String field, String value) {
  66.         List<Document> records = executeSelect(new BasicDBObject(field, value));
  67.         return records;
  68.     }
  69.    
  70.     public List<Document> getFieldValueNotEqualTo(String field, String value) {
  71.         List<Document> records = executeSelect(new BasicDBObject(field, new BasicDBObject("$ne", value)));
  72.         return records;
  73.     }
  74.    
  75.     public static void printRecords(List<Document> records) {
  76.         for (Document record : records) {
  77.             System.out.println(record.toJson());   
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement