private List<String> selectViewCodes(String schema) throws UnknownHostException { // Initiate variables List<String> l = new ArrayList<String>(); MongoDBConnectionManager mgr = MongoDBConnectionManager.getInstance(); Mongo mongo = mgr.getMongo(); DB db = mongo.getDB(schema); DBCollection collection = db.getCollection("views"); // Compose NoSQL query BasicDBObject groupFiels = new BasicDBObject("_id", "$view_id"); BasicDBObject group = new BasicDBObject("$group", groupFiels); BasicDBObject sortFiels = new BasicDBObject("_id", 1); BasicDBObject sort = new BasicDBObject("$sort", sortFiels); // Create the output AggregationOutput output = collection.aggregate(group, sort); Iterable<DBObject> results = output.results(); for (DBObject result : results) { try { l.add(result.get("_id").toString()); } catch (NullPointerException e) { System.out.println("Skipping NULL"); } } // Return the result return l; }
private String selectBooks(String collectionType, String type, String text, String schema) throws UnknownHostException { System.out.println(type); System.out.println(schema); List<String> l = new ArrayList<String>(); MongoDBConnectionManager mgr = MongoDBConnectionManager.getInstance(); Mongo mongo = mgr.getMongo(); DB db = mongo.getDB(schema); // collectionType = "view" try { DBCollection collection = db.getCollection(collectionType); BasicDBObject searchQuery = new BasicDBObject(); BasicDBObject regexQuery = new BasicDBObject(); // regexQuery.put("title", new BasicDBObject("$regex", "^.*mi.*$").append("$options", "i")); regexQuery.put(type, new BasicDBObject("$regex", text).append("$options", "i")); DBCursor cursor = collection.find(regexQuery); while (cursor.hasNext()) l.add(cursor.next().toString()); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(l); if (l.size() > 0) return l.toString(); return "error"; }
private String selectBooks( String schema, String collection, String title, String author, String isbn) throws UnknownHostException { System.out.println(schema); System.out.println(collection); System.out.println(title); System.out.println(author); List<String> l = new ArrayList<String>(); MongoDBConnectionManager mgr = MongoDBConnectionManager.getInstance(); Mongo mongo = mgr.getMongo(); DB db = mongo.getDB(schema); try { DBCollection c = db.getCollection(collection); BasicDBObject titleQuery = new BasicDBObject(); titleQuery.put("title", new BasicDBObject("$regex", title)); BasicDBObject authorQuery = new BasicDBObject(); authorQuery.put("author", new BasicDBObject("$regex", author)); BasicDBList or = new BasicDBList(); or.add(titleQuery); or.add(titleQuery); DBObject query = new BasicDBObject("$and", or); DBCursor cursor = c.find(titleQuery); while (cursor.hasNext()) l.add(cursor.next().toString()); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(l); if (l.size() > 0) return l.toString(); else return null; }
private void saveJson(String schema, String collection, String json) throws UnknownHostException { MongoDBConnectionManager mgr = MongoDBConnectionManager.getInstance(); Mongo mongo = mgr.getMongo(); DB db = mongo.getDB(schema); DBCollection c = db.getCollection(collection); DBObject dbObject = (DBObject) JSON.parse(json); c.insert(dbObject); }