Example usage for com.mongodb.client MongoDatabase getCollection

List of usage examples for com.mongodb.client MongoDatabase getCollection

Introduction

In this page you can find the example usage for com.mongodb.client MongoDatabase getCollection.

Prototype

MongoCollection<Document> getCollection(String collectionName);

Source Link

Document

Gets a collection.

Usage

From source file:MyLibrary.DoMongodb.java

/**
 * mongodb//from  w  w  w.j a v  a 2  s .c  o  m
 * @param MongoDatabase1 MongoDatabase
 * @param collections collections
 * @param JsonObject1 
 * @param JsonObject2 
 * @param isUpsert 
 * @return null
 */
public String update(MongoDatabase MongoDatabase1, String collections, JSONObject JsonObject1,
        JSONObject JsonObject2, boolean isUpsert) {
    String result = null;
    Document Document1 = new Document();
    Document Document2 = new Document();
    try {
        Document1 = JsonToDocument(JsonObject1);
        Document2 = JsonToDocument(JsonObject2);
        UpdateOptions UpdateOptions1 = new UpdateOptions();
        UpdateOptions1.upsert(isUpsert);
        MongoDatabase1.getCollection(collections).updateOne(Document1, new Document("$set", Document2),
                UpdateOptions1);
    } catch (Exception e) {
        result = e.getMessage();
    }
    return result;
}

From source file:MyLibrary.DoMongodb.java

public List<Document> doMongodbFind(MongoDatabase MongoDatabase1, String collections, JSONObject JsonObject1)
        throws Exception {
    List<Document> result = new ArrayList<>();
    Document Document1 = new Document();
    Document1 = JsonToDocument(JsonObject1);
    FindIterable FindIterable1 = MongoDatabase1.getCollection(collections).find(Document1);
    Iterator Iterator1 = FindIterable1.iterator();
    while (Iterator1.hasNext()) {
        Document next = (Document) Iterator1.next();
        result.add(next);/*w  w  w.  j  a  v a 2s .com*/
    }
    return result;
}

From source file:MyLibrary.DoMongodb.java

public List<Document> doMongodbAggregation(MongoDatabase MongoDatabase1, String collections,
        JSONObject matchJSONObject, JSONObject JsonObject1) throws Exception {
    final List<Document> result = new ArrayList<>();
    Document groupDocument = JsonToDocument(JsonObject1);
    Document matchDocument = JsonToDocument(matchJSONObject);
    AggregateIterable<Document> AggregateIterable1 = MongoDatabase1.getCollection(collections)
            .aggregate(asList(new Document("$match", matchDocument), new Document("$group", groupDocument)));
    AggregateIterable1.forEach(new Block<Document>() {
        @Override//  w  w  w .  ja  v  a2 s  .  c  o m
        public void apply(final Document document) {
            result.add(document);
        }
    });
    return result;
}

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);//from   w  w  w.j  a  va 2s. 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;//from  w  w w .j  a  va 2s.  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:net.netzgut.integral.mongo.internal.services.MongoServiceImplementation.java

License:Apache License

@Override
public MongoCollection<Document> getCollection(MongoDatabase db, String collectionName,
        boolean ignoreNamingStrategy) {
    if (db == null) {
        log.error("Database can't be null");
        throw new IllegalArgumentException("Database can't be null");
    }/*from  w w w. j a v  a 2  s  .c o m*/

    if (collectionName == null || collectionName.length() == 0) {
        log.error("Collection name can't be blank");
        throw new IllegalArgumentException("Collection name can't be blank");
    }

    String finalCollectionName = ignoreNamingStrategy ? collectionName : getCollectionName(collectionName);

    return db.getCollection(finalCollectionName);
}

From source file:net.netzgut.integral.mongo.internal.services.MongoServiceImplementation.java

License:Apache License

@Override
public void capCollection(MongoDatabase db, String collectionName, long sizeInBytes) {
    final MongoIterable<String> result = db.listCollectionNames();
    final List<String> names = result.into(new ArrayList<>());

    if (names.contains(collectionName)) {
        final Document getStats = new Document("collStats", collectionName);
        final Document stats = db.runCommand(getStats);
        Object capped = stats.get("capped");
        final boolean isCapped = capped != null && capped.equals(1);
        if (isCapped == false) {
            final Document convertToCapped = new Document();
            convertToCapped.append("convertToCapped", collectionName);
            convertToCapped.append("size", sizeInBytes);
            db.runCommand(convertToCapped);

            // We need to create the index manually after conversion.
            // See red warning box: http://docs.mongodb.org/v2.2/reference/command/convertToCapped/#dbcmd.convertToCapped
            db.getCollection(collectionName).createIndex(new Document("_id", 1));
        }/*from   w ww.j  av  a2  s  . com*/
    } else {
        db.createCollection(collectionName,
                new CreateCollectionOptions().capped(true).sizeInBytes(sizeInBytes));
    }

}

From source file:nl.mvdb.mongodb.RemoveStudentsLowestHomeworkScore.java

License:Apache License

public static void main(String[] args) {
    MongoClient client = new MongoClient();
    try {/*from  w  ww.  ja v  a  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 {// ww w  .  j a  v a2s .  co 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();/*ww w .  ja v  a2 s. co  m*/

    // 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());
}