List of usage examples for com.mongodb MongoClient getDatabase
public MongoDatabase getDatabase(final String databaseName)
From source file:mx.com.tecnomotum.testmongodb.Principal.java
public static void main(String args[]) { MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase db = mongoClient.getDatabase("test"); MongoCollection<Document> coleccion = db.getCollection("restaurants"); long totalElementos = coleccion.count(); System.out.println("Total de elementos en la coleccin:" + totalElementos); // Obtener el primer elemento de la coleccin Document myDoc = coleccion.find().first(); System.out.println("Primer object:" + myDoc.toJson()); //Crear y aadir un nuevo documento a la coleccin Document nuevoDoc = new Document("name", "CARLITOS buf"); nuevoDoc.append("borough", "Elvia"); nuevoDoc.append("cuisine", "Gourmet"); List<Document> puntuaciones = new ArrayList<>(); Document punt = new Document(); punt.append("grade", "A"); punt.append("date", new Date()); punt.append("score", 9); puntuaciones.add(punt);/*from w w w . j a v a 2 s .c om*/ nuevoDoc.append("grades", puntuaciones); coleccion.insertOne(nuevoDoc); System.out.println("Total de elementos en la coleccin:" + coleccion.count()); //OBtener un objeto de una coleccin Document objetoResp = coleccion.find(eq("name", "CARLITOS buf")).first(); System.out.println("OBjeto encontrado:" + objetoResp.toJson()); //OBtener la proyeccin del documento Document objetoResp2 = coleccion.find(eq("name", "CARLITOS buf")) .projection(fields(excludeId(), include("name"), include("grades.score"))).first(); System.out.println("OBjeto encontrado:" + objetoResp2.toJson()); //OBtener conjuntos de datos Block<Document> printBlock = new Block<Document>() { @Override public void apply(final Document doc) { System.out.println(doc.toJson()); } }; coleccion.find(eq("cuisine", "Hamburgers")).projection(fields(excludeId(), include("name"))) .sort(Sorts.ascending("name")).forEach(printBlock); }
From source file:MyLibrary.DoMongodb.java
public MongoDatabase initMongodb(String database) throws Exception { MongoClient MongoClient1 = new MongoClient(); MongoDatabase MongoDatabase1 = MongoClient1.getDatabase(database); return MongoDatabase1; }
From source file:mypackage.CreateDB.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { String getDBName = request.getParameter("dbname"); MongoClient mongoClient = new MongoClient("localhost", 27017); HttpSession httpSession = request.getSession(false); DB db = mongoClient.getDB("mydb"); DBCollection dBCollection = db.getCollection(httpSession.getAttribute("uname") + "DB"); BasicDBObject dBObject = new BasicDBObject(); dBObject.put("kapil", getDBName); WriteResult writeResult = dBCollection.insert(dBObject); if (writeResult.getN() == 0) { out.print("true"); MongoDatabase mongoDatabase = mongoClient.getDatabase(getDBName); mongoDatabase.createCollection("Welcome"); mongoDatabase.drop();//from w w w .ja va 2 s . c o m } else { out.print("false"); } } }
From source file:mypackage.DBInformation.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { try {/*from w w w. ja va 2 s. c om*/ String dbName = request.getParameter("dbname"); MongoClient mongoClient = new MongoClient("localhost", 27017); HttpSession httpSession = request.getSession(false); MongoDatabase mongoDatabase = mongoClient.getDatabase(dbName); MongoIterable<String> mongoIterable = mongoDatabase.listCollectionNames(); MongoCursor<String> mongoCursor = mongoIterable.iterator(); JSONObject jSONObject = new JSONObject(); JSONObject jSONObject1 = new JSONObject(); JSONArray jSONArray = new JSONArray(); int i = 0; while (mongoCursor.hasNext()) { jSONArray.put(mongoCursor.next()); i++; } jSONObject.put("db", jSONArray); jSONObject.put("counter", i); out.println(jSONObject); } catch (JSONException e) { } } }
From source file:net.modelbased.proasense.storage.reader.EventReaderMongoSync.java
License:Apache License
public List<Document> call() { // Connect to MongoDB database MongoClient mongoClient = new MongoClient(new MongoClientURI(this.mongoURL)); MongoDatabase database = mongoClient.getDatabase(this.database); MongoCollection<Document> collection = database.getCollection(this.collectionId); // Create document list for query result List<Document> foundDocuments = new ArrayList<Document>(); if (queryType.equals(EventQueryType.SIMPLE) && queryOperation.equals(EventQueryOperation.DEFAULT)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { Document doc = cursor.next(); foundDocuments.add(doc);//ww w .jav a 2 s . c o m } } if (queryType.equals(EventQueryType.SIMPLE) && queryOperation.equals(EventQueryOperation.AVERAGE)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); boolean longProperty = false; boolean doubleProperty = false; MongoCursor<Document> firstCursor = it.iterator(); if (firstCursor.hasNext()) { Document doc = firstCursor.next(); Document eventProps = (Document) doc.get("eventProperties"); Object valueObj = eventProps.get(this.propertyKey); if (valueObj instanceof Long) longProperty = true; else if (valueObj instanceof Double) doubleProperty = true; } long resultAverageLong = 0; double resultAverageDouble = 0; long collectionSize = 0; MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { collectionSize++; Document doc = cursor.next(); Document eventProps = (Document) doc.get("eventProperties"); if (longProperty) { Long value = (Long) eventProps.get(this.propertyKey); resultAverageLong = resultAverageLong + value; } else if (doubleProperty) { Double value = (Double) eventProps.get(this.propertyKey); resultAverageDouble = resultAverageDouble + value; doubleProperty = true; } } if (longProperty) { Long resultAverage = resultAverageLong / collectionSize; Document resultDoc = new Document("RESULT", resultAverage); foundDocuments.add(resultDoc); } else if (doubleProperty) { Double resultAverage = resultAverageDouble / collectionSize; Document resultDoc = new Document("RESULT", resultAverage); foundDocuments.add(resultDoc); } } if (queryType.equals(EventQueryType.SIMPLE) && queryOperation.equals(EventQueryOperation.MAXIMUM)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); boolean longProperty = false; boolean doubleProperty = false; MongoCursor<Document> firstCursor = it.iterator(); if (firstCursor.hasNext()) { Document doc = firstCursor.next(); Document eventProps = (Document) doc.get("eventProperties"); Object valueObj = eventProps.get(this.propertyKey); if (valueObj instanceof Long) longProperty = true; else if (valueObj instanceof Double) doubleProperty = true; } long resultMaximumLong = Long.MIN_VALUE; double resultMaximumDouble = Double.MIN_VALUE; MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { Document doc = cursor.next(); Document eventProps = (Document) doc.get("eventProperties"); if (longProperty) { long value = (Long) eventProps.get(this.propertyKey); if (value > resultMaximumLong) resultMaximumLong = value; } else if (doubleProperty) { double value = (Double) eventProps.get(this.propertyKey); if (value > resultMaximumDouble) resultMaximumDouble = value; } } if (longProperty) { Long resultMaximum = resultMaximumLong; Document resultDoc = new Document("RESULT", resultMaximum); foundDocuments.add(resultDoc); } else if (doubleProperty) { Double resultMaximum = resultMaximumDouble; Document resultDoc = new Document("RESULT", resultMaximum); foundDocuments.add(resultDoc); } } if (queryType.equals(EventQueryType.SIMPLE) && queryOperation.equals(EventQueryOperation.MINUMUM)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); boolean longProperty = false; boolean doubleProperty = false; MongoCursor<Document> firstCursor = it.iterator(); if (firstCursor.hasNext()) { Document doc = firstCursor.next(); Document eventProps = (Document) doc.get("eventProperties"); Object valueObj = eventProps.get(this.propertyKey); if (valueObj instanceof Long) longProperty = true; else if (valueObj instanceof Double) doubleProperty = true; } long resultMinimumLong = Long.MAX_VALUE; double resultMinimumDouble = Double.MAX_VALUE; MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { Document doc = cursor.next(); Document eventProps = (Document) doc.get("eventProperties"); if (longProperty) { long value = (Long) eventProps.get(this.propertyKey); if (value < resultMinimumLong) resultMinimumLong = value; } else if (doubleProperty) { Double value = (Double) eventProps.get(this.propertyKey); if (value < resultMinimumDouble) resultMinimumDouble = value; } } if (longProperty) { long resultMinimum = resultMinimumLong; Document resultDoc = new Document("RESULT", resultMinimum); foundDocuments.add(resultDoc); } else if (doubleProperty) { double resultMinimum = resultMinimumDouble; Document resultDoc = new Document("RESULT", resultMinimum); foundDocuments.add(resultDoc); } } if (queryType.equals(EventQueryType.DERIVED) && queryOperation.equals(EventQueryOperation.DEFAULT)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { Document doc = cursor.next(); foundDocuments.add(doc); } } if (queryType.equals(EventQueryType.DERIVED) && queryOperation.equals(EventQueryOperation.AVERAGE)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); boolean longProperty = false; boolean doubleProperty = false; MongoCursor<Document> firstCursor = it.iterator(); if (firstCursor.hasNext()) { Document doc = firstCursor.next(); Document eventProps = (Document) doc.get("eventProperties"); Object valueObj = eventProps.get(this.propertyKey); if (valueObj instanceof Long) longProperty = true; else if (valueObj instanceof Double) doubleProperty = true; } long resultAverageLong = 0; double resultAverageDouble = 0; long collectionSize = 0; MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { collectionSize++; Document doc = cursor.next(); Document eventProps = (Document) doc.get("eventProperties"); if (longProperty) { Long value = (Long) eventProps.get(this.propertyKey); resultAverageLong = resultAverageLong + value; } else if (doubleProperty) { Double value = (Double) eventProps.get(this.propertyKey); resultAverageDouble = resultAverageDouble + value; } } if (longProperty) { Long resultAverage = resultAverageLong / collectionSize; Document resultDoc = new Document("RESULT", resultAverage); foundDocuments.add(resultDoc); } else if (doubleProperty) { Double resultAverage = resultAverageDouble / collectionSize; Document resultDoc = new Document("RESULT", resultAverage); foundDocuments.add(resultDoc); } } if (queryType.equals(EventQueryType.DERIVED) && queryOperation.equals(EventQueryOperation.MAXIMUM)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); boolean longProperty = false; boolean doubleProperty = false; MongoCursor<Document> firstCursor = it.iterator(); if (firstCursor.hasNext()) { Document doc = firstCursor.next(); Document eventProps = (Document) doc.get("eventProperties"); Object valueObj = eventProps.get(this.propertyKey); if (valueObj instanceof Long) longProperty = true; else if (valueObj instanceof Double) doubleProperty = true; } long resultMaximumLong = Long.MIN_VALUE; double resultMaximumDouble = Double.MIN_VALUE; MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { Document doc = cursor.next(); Document eventProps = (Document) doc.get("eventProperties"); if (longProperty) { long value = (Long) eventProps.get(this.propertyKey); if (value > resultMaximumLong) resultMaximumLong = value; } else if (doubleProperty) { double value = (Double) eventProps.get(this.propertyKey); if (value > resultMaximumDouble) resultMaximumDouble = value; doubleProperty = true; } } if (longProperty) { long resultMaximum = resultMaximumLong; Document resultDoc = new Document("RESULT", resultMaximum); foundDocuments.add(resultDoc); } else if (doubleProperty) { double resultMaximum = resultMaximumDouble; Document resultDoc = new Document("RESULT", resultMaximum); foundDocuments.add(resultDoc); } } if (queryType.equals(EventQueryType.DERIVED) && queryOperation.equals(EventQueryOperation.MINUMUM)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); boolean longProperty = false; boolean doubleProperty = false; MongoCursor<Document> firstCursor = it.iterator(); if (firstCursor.hasNext()) { Document doc = firstCursor.next(); Document eventProps = (Document) doc.get("eventProperties"); Object valueObj = eventProps.get(this.propertyKey); if (valueObj instanceof Long) longProperty = true; else if (valueObj instanceof Double) doubleProperty = true; } long resultMinimumLong = Long.MAX_VALUE; double resultMinimumDouble = Double.MAX_VALUE; MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { Document doc = cursor.next(); Document eventProps = (Document) doc.get("eventProperties"); if (longProperty) { long value = (Long) eventProps.get(this.propertyKey); if (value < resultMinimumLong) resultMinimumLong = value; } else if (doubleProperty) { double value = (Double) eventProps.get(this.propertyKey); if (value < resultMinimumDouble) resultMinimumDouble = value; } } if (longProperty) { long resultMinimum = resultMinimumLong; Document resultDoc = new Document("RESULT", resultMinimum); foundDocuments.add(resultDoc); } else if (doubleProperty) { double resultMinimum = resultMinimumDouble; Document resultDoc = new Document("RESULT", resultMinimum); foundDocuments.add(resultDoc); } } if (queryType.equals(EventQueryType.PREDICTED) && queryOperation.equals(EventQueryOperation.DEFAULT)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { Document doc = cursor.next(); foundDocuments.add(doc); } } if (queryType.equals(EventQueryType.PREDICTED) && queryOperation.equals(EventQueryOperation.AVERAGE)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); boolean longProperty = false; boolean doubleProperty = false; MongoCursor<Document> firstCursor = it.iterator(); if (firstCursor.hasNext()) { Document doc = firstCursor.next(); Document eventProps = (Document) doc.get("eventProperties"); Object valueObj = eventProps.get(this.propertyKey); if (valueObj instanceof Long) longProperty = true; else if (valueObj instanceof Double) doubleProperty = true; } long resultAverageLong = 0; double resultAverageDouble = 0; long collectionSize = 0; MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { collectionSize++; Document doc = cursor.next(); Document eventProps = (Document) doc.get("eventProperties"); if (longProperty) { long value = (Long) eventProps.get(this.propertyKey); resultAverageLong = resultAverageLong + value; } else if (doubleProperty) { double value = (Double) eventProps.get(this.propertyKey); resultAverageDouble = resultAverageDouble + value; } } if (longProperty) { long resultAverage = resultAverageLong / collectionSize; Document resultDoc = new Document("RESULT", resultAverage); foundDocuments.add(resultDoc); } else if (doubleProperty) { double resultAverage = resultAverageDouble / collectionSize; Document resultDoc = new Document("RESULT", resultAverage); foundDocuments.add(resultDoc); } } if (queryType.equals(EventQueryType.PREDICTED) && queryOperation.equals(EventQueryOperation.MAXIMUM)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); boolean longProperty = false; boolean doubleProperty = false; MongoCursor<Document> firstCursor = it.iterator(); if (firstCursor.hasNext()) { Document doc = firstCursor.next(); Document eventProps = (Document) doc.get("eventProperties"); Object valueObj = eventProps.get(this.propertyKey); if (valueObj instanceof Long) longProperty = true; else if (valueObj instanceof Double) doubleProperty = true; } long resultMaximumLong = Long.MIN_VALUE; double resultMaximumDouble = Double.MIN_VALUE; MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { Document doc = cursor.next(); Document eventProps = (Document) doc.get("eventProperties"); if (longProperty) { long value = (Long) eventProps.get(this.propertyKey); if (value > resultMaximumLong) resultMaximumLong = value; } else if (doubleProperty) { double value = (Double) eventProps.get(this.propertyKey); if (value > resultMaximumDouble) resultMaximumDouble = value; } } if (longProperty) { long resultMaximum = resultMaximumLong; Document resultDoc = new Document("RESULT", resultMaximum); foundDocuments.add(resultDoc); } else if (doubleProperty) { double resultMaximum = resultMaximumDouble; Document resultDoc = new Document("RESULT", resultMaximum); foundDocuments.add(resultDoc); } } if (queryType.equals(EventQueryType.PREDICTED) && queryOperation.equals(EventQueryOperation.MINUMUM)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); boolean longProperty = false; boolean doubleProperty = false; MongoCursor<Document> firstCursor = it.iterator(); if (firstCursor.hasNext()) { Document doc = firstCursor.next(); Document eventProps = (Document) doc.get("eventProperties"); Object valueObj = eventProps.get(this.propertyKey); if (valueObj instanceof Long) longProperty = true; else if (valueObj instanceof Double) doubleProperty = true; } long resultMinimumLong = Long.MAX_VALUE; double resultMinimumDouble = Double.MAX_VALUE; MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { Document doc = cursor.next(); Document eventProps = (Document) doc.get("eventProperties"); if (longProperty) { long value = (Long) eventProps.get(this.propertyKey); if (value < resultMinimumLong) resultMinimumLong = value; } else if (doubleProperty) { double value = (Long) eventProps.get(this.propertyKey); if (value < resultMinimumDouble) resultMinimumDouble = value; } } if (longProperty) { long resultMinimum = resultMinimumLong; Document resultDoc = new Document("RESULT", resultMinimum); foundDocuments.add(resultDoc); } else if (doubleProperty) { double resultMinimum = resultMinimumDouble; Document resultDoc = new Document("RESULT", resultMinimum); foundDocuments.add(resultDoc); } } if (queryType.equals(EventQueryType.ANOMALY) && queryOperation.equals(EventQueryOperation.DEFAULT)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { Document doc = cursor.next(); foundDocuments.add(doc); } } if (queryType.equals(EventQueryType.RECOMMENDATION) && queryOperation.equals(EventQueryOperation.DEFAULT)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { Document doc = cursor.next(); foundDocuments.add(doc); } } if (queryType.equals(EventQueryType.RECOMMENDATION) && queryOperation.equals(EventQueryOperation.AVERAGE)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); boolean longProperty = false; boolean doubleProperty = false; MongoCursor<Document> firstCursor = it.iterator(); if (firstCursor.hasNext()) { Document doc = firstCursor.next(); Document eventProps = (Document) doc.get("eventProperties"); Object valueObj = eventProps.get(this.propertyKey); if (valueObj instanceof Long) longProperty = true; else if (valueObj instanceof Double) doubleProperty = true; } long resultAverageLong = 0; double resultAverageDouble = 0; long collectionSize = 0; MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { collectionSize++; Document doc = cursor.next(); Document eventProps = (Document) doc.get("eventProperties"); if (longProperty) { long value = (Long) eventProps.get(this.propertyKey); resultAverageLong = resultAverageLong + value; } else if (doubleProperty) { double value = (Double) eventProps.get(this.propertyKey); resultAverageDouble = resultAverageDouble + value; } } if (longProperty) { long resultAverage = resultAverageLong / collectionSize; Document resultDoc = new Document("RESULT", resultAverage); foundDocuments.add(resultDoc); } else if (doubleProperty) { double resultAverage = resultAverageDouble / collectionSize; Document resultDoc = new Document("RESULT", resultAverage); foundDocuments.add(resultDoc); } } if (queryType.equals(EventQueryType.RECOMMENDATION) && queryOperation.equals(EventQueryOperation.MAXIMUM)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); boolean longProperty = false; boolean doubleProperty = false; MongoCursor<Document> firstCursor = it.iterator(); if (firstCursor.hasNext()) { Document doc = firstCursor.next(); Document eventProps = (Document) doc.get("eventProperties"); Object valueObj = eventProps.get(this.propertyKey); if (valueObj instanceof Long) longProperty = true; else if (valueObj instanceof Double) doubleProperty = true; } long resultMaximumLong = Long.MIN_VALUE; double resultMaximumDouble = Double.MIN_VALUE; MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { Document doc = cursor.next(); Document eventProps = (Document) doc.get("eventProperties"); if (longProperty) { long value = (Long) eventProps.get(this.propertyKey); if (value > resultMaximumLong) resultMaximumLong = value; } else if (doubleProperty) { double value = (Double) eventProps.get(this.propertyKey); if (value > resultMaximumDouble) resultMaximumDouble = value; } } if (longProperty) { long resultMaximum = resultMaximumLong; Document resultDoc = new Document("RESULT", resultMaximum); foundDocuments.add(resultDoc); } else if (doubleProperty) { double resultMaximum = resultMaximumDouble; Document resultDoc = new Document("RESULT", resultMaximum); foundDocuments.add(resultDoc); } } if (queryType.equals(EventQueryType.RECOMMENDATION) && queryOperation.equals(EventQueryOperation.MINUMUM)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); boolean longProperty = false; boolean doubleProperty = false; MongoCursor<Document> firstCursor = it.iterator(); if (firstCursor.hasNext()) { Document doc = firstCursor.next(); Document eventProps = (Document) doc.get("eventProperties"); Object valueObj = eventProps.get(this.propertyKey); if (valueObj instanceof Long) longProperty = true; else if (valueObj instanceof Double) doubleProperty = true; } long resultMinimumLong = Long.MAX_VALUE; double resultMinimumDouble = Double.MAX_VALUE; MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { Document doc = cursor.next(); Document eventProps = (Document) doc.get("eventProperties"); if (longProperty) { long value = (Long) eventProps.get(this.propertyKey); if (value < resultMinimumLong) resultMinimumLong = value; } else if (doubleProperty) { double value = (Double) eventProps.get(this.propertyKey); if (value < resultMinimumDouble) resultMinimumDouble = value; } } if (longProperty) { long resultMinimum = resultMinimumLong; Document resultDoc = new Document("RESULT", resultMinimum); foundDocuments.add(resultDoc); } else if (doubleProperty) { double resultMinimum = resultMinimumDouble; Document resultDoc = new Document("RESULT", resultMinimum); foundDocuments.add(resultDoc); } } if (queryType.equals(EventQueryType.FEEDBACK) && queryOperation.equals(EventQueryOperation.DEFAULT)) { FindIterable<Document> it = collection .find(and(gte("timestamp", this.startTime), lte("timestamp", this.endTime))); MongoCursor<Document> cursor = it.iterator(); while (cursor.hasNext()) { Document doc = cursor.next(); foundDocuments.add(doc); } } mongoClient.close(); return foundDocuments; }
From source file:net.modelbased.proasense.storage.writer.EventWriterMongoSync.java
License:Apache License
public void run() { // Connect to MongoDB database MongoClient mongoClient = new MongoClient(new MongoClientURI(mongoURL)); MongoDatabase database = mongoClient.getDatabase(EventProperties.STORAGE_DATABASE_NAME); // Create hash map of collections Map<String, MongoCollection<Document>> collectionMap = new HashMap<String, MongoCollection<Document>>(); int cnt = 0;/* w ww . j a v a 2 s . c om*/ long timer0 = System.currentTimeMillis(); long timer1 = timer0; long timer2 = timer0; boolean skipLog = false; Map<String, List<Document>> documentMap = new HashMap<String, List<Document>>(); try { if (isLogfile) logfileWriter = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("EventWriterMongoSync_benchmark_" + this.threadNumber + ".txt"), "ISO-8859-1")); while (true) { long timeoutExpired = System.currentTimeMillis() + this.maxWait; EventDocument eventDocument = queue.take(); String collectionId = eventDocument.getCollectionId(); if (!collectionId.matches(EventProperties.STORAGE_HEARTBEAT)) { cnt++; skipLog = false; } else skipLog = true; // Add data for bulk write if (!collectionId.matches(EventProperties.STORAGE_HEARTBEAT)) { Document document = eventDocument.getDocument(); if (!collectionMap.containsKey(collectionId)) { collectionMap.put(collectionId, database.getCollection(collectionId)); List<Document> documentList = new ArrayList<Document>(); documentMap.put(collectionId, documentList); } documentMap.get(collectionId).add(document); } // Write data if heartbeat timeout is received else { for (Map.Entry<String, MongoCollection<Document>> entry : collectionMap.entrySet()) { String key = entry.getKey(); if (!documentMap.get(key).isEmpty()) { collectionMap.get(key).insertMany(documentMap.get(key)); documentMap.get(key).clear(); } } } // Write data if bulk size or max wait (ms) is reached if ((cnt % this.bulkSize == 0) || (System.currentTimeMillis() >= timeoutExpired)) { for (Map.Entry<String, MongoCollection<Document>> entry : collectionMap.entrySet()) { String key = entry.getKey(); if (!documentMap.get(key).isEmpty()) { collectionMap.get(key).insertMany(documentMap.get(key)); documentMap.get(key).clear(); } } } // Benchmark output if ((!skipLog) && (cnt % this.logSize == 0)) { timer2 = System.currentTimeMillis(); long difference = timer2 - timer1; if (difference != 0) { long average = (this.logSize * 1000) / (timer2 - timer1); System.out.println("Benchmark: "); System.out.println(" Records written : " + cnt); System.out.println(" Average records/s: " + average); timer1 = timer2; if (isLogfile) { logfileWriter.write(cnt + "," + average + System.getProperty("line.separator")); logfileWriter.flush(); } if (cnt == this.loadTestMaxMessages) { long loadTestStart = timer0 / 1000; long loadTestEnd = timer2 / 1000; long loadTestTime = loadTestEnd - loadTestStart; long loadTestAverage = this.loadTestMaxMessages / loadTestTime; System.out.println("*****************************"); System.out.println("Load test results: "); System.out.println(" Records written : " + cnt); System.out.println(" Average records/s: " + loadTestAverage); if (isLogfile) { logfileWriter.write( "*****************************" + System.getProperty("line.separator")); logfileWriter.write("Load test results: " + System.getProperty("line.separator")); logfileWriter.write( " Records written : " + cnt + System.getProperty("line.separator")); logfileWriter.write(" Average records/s: " + loadTestAverage + System.getProperty("line.separator")); logfileWriter.flush(); } } } } } } catch (InterruptedException e) { System.out.println(e.getClass().getName() + ": " + e.getMessage()); } catch (IOException e) { System.out.println(e.getClass().getName() + ": " + e.getMessage()); } finally { if (isLogfile) try { logfileWriter.close(); } catch (IOException e) { System.out.println(e.getClass().getName() + ": " + e.getMessage()); } } mongoClient.close(); }
From source file:nl.mvdb.mongodb.RemoveStudentsLowestHomeworkScore.java
License:Apache License
public static void main(String[] args) { MongoClient client = new MongoClient(); try {// ww w. j a va 2s . c o m MongoDatabase database = client.getDatabase("school"); MongoCollection<Document> collection = database.getCollection("students"); MongoCursor<Document> iterator = collection.find().sort(ascending("scores.score")).iterator(); while (iterator.hasNext()) { Document studentDoc = iterator.next(); System.out.println(studentDoc); Document lowestScoreDoc = null; List<Document> scoreDocs = (ArrayList) studentDoc.get("scores"); for (Document scoreDoc : scoreDocs) { if ("homework".equals(scoreDoc.getString("type"))) { Double score = scoreDoc.getDouble("score"); if (lowestScoreDoc == null || score < lowestScoreDoc.getDouble("score")) { lowestScoreDoc = scoreDoc; } } } if (lowestScoreDoc != null) { scoreDocs.remove(lowestScoreDoc); studentDoc.put("scores", scoreDocs); collection.replaceOne(new Document("_id", studentDoc.getInteger("_id")), studentDoc); } } System.out.println(collection.count()); } finally { client.close(); } }
From source file:nl.mvdb.mongodb.RemoveStudentsLowestScore.java
License:Apache License
public static void main(String[] args) { MongoClient client = new MongoClient(); try {//from ww w .j av a 2 s . c o m MongoDatabase database = client.getDatabase("students"); MongoCollection<Document> collection = database.getCollection("grades"); MongoCursor<Document> iterator = collection.find().sort(ascending("student_id", "score")).iterator(); Integer lastStudentId = null; while (iterator.hasNext()) { Document gradingDoc = iterator.next(); Integer studentId = gradingDoc.getInteger("student_id"); if (!studentId.equals(lastStudentId)) { lastStudentId = studentId; System.out.println(lastStudentId); collection.deleteOne(gradingDoc); } } System.out.println(collection.count()); } finally { client.close(); } }
From source file:nl.syntouch.oracle.adapter.cloud.mongodb.sample.MongoDBSample.java
License:Apache License
public static void main(String[] args) { // connect to MongoDB at localhost:27017 MongoClient mongoClient = new MongoClient(); // switch to test db MongoDatabase database = mongoClient.getDatabase("test"); // drop test collection (note the collection will always be created by the getCollection command) MongoCollection<Document> collection = database.getCollection("test"); collection.drop();/* w w w. j a v a2 s . c om*/ // create a new collection collection = database.getCollection("test"); // create BSON document and save it Document doc = new Document().append("field1", "value1"); collection.insertOne(doc); System.out.println(doc.toString()); Document queryDoc1 = new Document().append("_id", doc.get("_id")); System.out.println(collection.find(queryDoc1).first().toString()); Document queryDoc2 = new Document().append("field1", doc.get("field1")); System.out.println(collection.find(queryDoc2).first().toString()); }
From source file:okra.index.IndexCreator.java
License:Open Source License
public static <T extends OkraItem> void ensureIndexes(final Okra<T> okra, final MongoClient mongo, final String database, final String collection) { okra.getIndexDefs().stream().map(indexDef -> { final boolean ascending = indexDef.getOrdering() == null || indexDef.getOrdering().equals(Ordering.ASC); final Bson ordering = ascending ? Indexes.ascending(indexDef.getAttrs()) : Indexes.descending(indexDef.getAttrs()); return mongo.getDatabase(database).getCollection(collection).createIndex(ordering); }).forEach(indexName -> LOGGER.info("Done. Index name: {}", indexName)); }