Example usage for com.mongodb.client MongoCollection find

List of usage examples for com.mongodb.client MongoCollection find

Introduction

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

Prototype

FindIterable<TDocument> find();

Source Link

Document

Finds all documents in the collection.

Usage

From source file:io.debezium.connector.mongodb.Replicator.java

License:Apache License

/**
 * Obtain the current position of the oplog, and record it in the source.
 *//*from w w  w .  j ava  2 s .  c o m*/
protected void recordCurrentOplogPosition() {
    primaryClient.execute("get oplog position", primary -> {
        MongoCollection<Document> oplog = primary.getDatabase("local").getCollection("oplog.rs");
        Document last = oplog.find().sort(new Document("$natural", -1)).limit(1).first(); // may be null
        source.offsetStructForEvent(replicaSet.replicaSetName(), last);
    });
}

From source file:io.debezium.connector.mongodb.Replicator.java

License:Apache License

/**
 * Determine if an initial sync should be performed. An initial sync is expected if the {@link #source} has no previously
 * recorded offsets for this replica set, or if {@link ReplicationContext#performSnapshotEvenIfNotNeeded() a snapshot should
 * always be performed}.//from  w ww.jav  a2s  .com
 * 
 * @return {@code true} if the initial sync should be performed, or {@code false} otherwise
 */
protected boolean isInitialSyncExpected() {
    boolean performSnapshot = true;
    if (source.hasOffset(rsName)) {
        logger.info("Found existing offset for replica set '{}' at {}", rsName, source.lastOffset(rsName));
        performSnapshot = false;
        if (context.performSnapshotEvenIfNotNeeded()) {
            logger.info("Configured to performing initial sync of replica set '{}'", rsName);
            performSnapshot = true;
        } else {
            if (source.isInitialSyncOngoing(rsName)) {
                // The last snapshot was not completed, so do it again ...
                logger.info(
                        "The previous initial sync was incomplete for '{}', so initiating another initial sync",
                        rsName);
                performSnapshot = true;
            } else {
                // There is no ongoing initial sync, so look to see if our last recorded offset still exists in the oplog.
                BsonTimestamp lastRecordedTs = source.lastOffsetTimestamp(rsName);
                AtomicReference<BsonTimestamp> firstExistingTs = new AtomicReference<>();
                primaryClient.execute("get oplog position", primary -> {
                    MongoCollection<Document> oplog = primary.getDatabase("local").getCollection("oplog.rs");
                    Document firstEvent = oplog.find().sort(new Document("$natural", 1)).limit(1).first(); // may be null
                    firstExistingTs.set(SourceInfo.extractEventTimestamp(firstEvent));
                });
                BsonTimestamp firstAvailableTs = firstExistingTs.get();
                if (firstAvailableTs == null) {
                    logger.info("The oplog contains no entries, so performing initial sync of replica set '{}'",
                            rsName);
                    performSnapshot = true;
                } else if (lastRecordedTs.compareTo(firstAvailableTs) < 0) {
                    // The last recorded timestamp is *before* the first existing oplog event, which means there is
                    // almost certainly some history lost since we last processed the oplog ...
                    logger.info(
                            "Initial sync is required since the oplog for replica set '{}' starts at {}, which is later than the timestamp of the last offset {}",
                            rsName, firstAvailableTs, lastRecordedTs);
                    performSnapshot = true;
                } else {
                    // Otherwise we'll not perform an initial sync
                    logger.info(
                            "The oplog contains the last entry previously read for '{}', so no initial sync will be performed",
                            rsName);
                }
            }
        }
    } else {
        logger.info("No existing offset found for replica set '{}', starting initial sync", rsName);
        performSnapshot = true;
    }
    return performSnapshot;
}

From source file:io.debezium.connector.mongodb.Replicator.java

License:Apache License

/**
 * Copy the collection, sending to the recorder a record for each document.
 * /*from w  ww . j  av  a2 s . co m*/
 * @param primary the connection to the replica set's primary node; may not be null
 * @param collectionId the identifier of the collection to be copied; may not be null
 * @param timestamp the timestamp in milliseconds at which the copy operation was started
 * @return number of documents that were copied
 * @throws InterruptedException if the thread was interrupted while the copy operation was running
 */
protected long copyCollection(MongoClient primary, CollectionId collectionId, long timestamp)
        throws InterruptedException {
    RecordsForCollection factory = recordMakers.forCollection(collectionId);
    MongoDatabase db = primary.getDatabase(collectionId.dbName());
    MongoCollection<Document> docCollection = db.getCollection(collectionId.name());
    long counter = 0;
    try (MongoCursor<Document> cursor = docCollection.find().iterator()) {
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            logger.trace("Found existing doc in {}: {}", collectionId, doc);
            counter += factory.recordObject(collectionId, doc, timestamp);
        }
    }
    return counter;
}

From source file:it.av.fac.dbi.util.FieldUpdater.java

public static void main(String[] args) {
    SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss Z yyyy");

    MongoClient mongoClient = new MongoClient("127.0.0.1", 27017);
    MongoDatabase mongoDB = mongoClient.getDatabase("test");
    MongoCollection<Document> collection = mongoDB.getCollection("tweets");

    FindIterable<Document> documents = collection.find();
    documents.forEach(new Consumer<Document>() {
        @Override/*from   w  ww .j a va 2  s .  co  m*/
        public void accept(Document doc) {
            try {
                //System.out.println(doc.toJson());
                Document user = (Document) doc.get("user");
                String dateStr = user.getString("created_at");
                //System.out.println(dateStr);
                Date date = parser.parse(dateStr);
                //System.out.println(date);
                System.out.println(collection.updateOne(eq("_id", doc.get("_id")),
                        new Document("$set", new Document("user.created_at", date))));
            } catch (ParseException ex) {
                Logger.getLogger(FieldUpdater.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });
}

From source file:it.terrinoni.Controller.java

public static void main(String[] args) {
    MongoClient client = new MongoClient();
    MongoDatabase database = client.getDatabase("photo-sharing");

    MongoCollection<Document> albums = database.getCollection("albums");
    MongoCollection<Document> images = database.getCollection("images");

    albums.createIndex(new Document("images", 1));

    // Get the iterator of the whole collection
    MongoCursor<Document> cursor = images.find().iterator();

    try {//w  w w. j  a  v a2 s .c  o m
        while (cursor.hasNext()) {
            Document currImg = cursor.next();
            Document foundImg = albums.find(eq("images", currImg.getDouble("_id"))).first();
            if (foundImg == null) {
                //System.out.println(currImg.getDouble("_id") + " deleted.");
                images.deleteOne(currImg);
            }
            //System.out.println(currImg.getDouble("_id") + " is ok.");
        }
    } finally {
        cursor.close();
    }

    long numImgs = images.count(eq("tags", "sunrises"));
    System.out.println("The total number of images with the tag \"sunrises\" after the removal of orphans is: "
            + String.valueOf(numImgs));
}

From source file:it.terrinoni.hw3.PruneHomeworks.java

public static void main(String[] args) {
    // MongoDB connection
    MongoClient client = new MongoClient();
    MongoDatabase database = client.getDatabase("school");
    MongoCollection<Document> collection = database.getCollection("students");

    // Get the cursor to the collection
    MongoCursor<Document> cursor = collection.find().iterator();

    try {/*from w ww .  ja  va  2s.  c om*/
        while (cursor.hasNext()) { // iteare over all the students
            double minScore = Double.MAX_VALUE; // set the maximum value
            Document minDoc = null; // temporary minimum
            Document student = cursor.next(); // current score

            // Retrieve the scores array
            List<Document> scores = student.get("scores", ArrayList.class);
            for (Document score : scores) { // iterate over the scores
                if (score.get("type", String.class).equals("homework")) { // get only the homeworks
                    System.out.println("Student " + student.getDouble("_id") + " has homework score equals to "
                            + score.getDouble("score"));
                    // Update the minimum score 
                    if (score.getDouble("score") < minScore) {
                        minScore = score.getDouble("score");
                        minDoc = score;
                    }
                }
            }
            // Remove the minimum score
            scores.remove(minDoc);

            // Update the student document
            Bson filter = eq("_id", student.getDouble("_id"));
            Document update = new Document("$set", new Document("scores", scores));
            collection.updateOne(filter, update);
        }
    } finally {
        cursor.close(); // close the cursos
    }
}

From source file:it.terrinoni.m101j.spark.HelloWorldMongoDBSparkFreemarkerStyle.java

public static void main(String[] args) {
    final Configuration configuration = new Configuration();
    configuration.setClassForTemplateLoading(HelloWorldMongoDBSparkFreemarkerStyle.class, "/freemarker");

    MongoClient client = new MongoClient();

    MongoDatabase database = client.getDatabase("course");
    final MongoCollection<Document> collection = database.getCollection("hello");

    collection.drop();/* w  w w. ja va 2  s .c om*/

    collection.insertOne(new Document("name", "MongoDB"));

    Spark.get("/", new Route() {
        @Override
        public Object handle(Request request, Response response) {
            StringWriter writer = new StringWriter();
            try {
                Template helloTemplate = configuration.getTemplate("hello.ftl");

                Document document = collection.find().first();

                helloTemplate.process(document, writer);

            } catch (IOException | TemplateException ex) {
                Spark.halt(500);
                ex.printStackTrace();
            }
            return writer;
        }
    });
}

From source file:javasensei.db.managments.BitacoraEjerciciosManager.java

public String obtenerBitacora(String idAlumno, String ejercicioId, String fechaInicial, String fechaFinal,
        String sesionId, String emocionInicial, String emocionFinal) {

    MongoCollection<DBObject> bitacoras = Connection.getDBV3().getCollection("bitacora_ejercicios",
            DBObject.class);
    Document query = new Document();

    if (!idAlumno.isEmpty()) {
        query.append("idAlumno", Integer.parseInt(idAlumno));
    }/*ww  w. j a  va2 s .c om*/

    if (!ejercicioId.isEmpty()) {
        query.append("ejercicioId", Integer.parseInt(ejercicioId));
    }

    if (!sesionId.isEmpty()) {
        query.append("sesionId", Integer.parseInt(sesionId));
    }
    //Variables para las fechas
    Date dateI;
    Date dateF;
    if (!fechaFinal.isEmpty() && !fechaInicial.isEmpty()) {

        dateI = Date.from(LocalDateTime.parse(fechaInicial).toInstant(ZoneOffset.UTC));
        dateF = Date.from(LocalDateTime.parse(fechaFinal).toInstant(ZoneOffset.UTC));
        query.append("fecha", new Document("$gte", dateI).append("$lte", dateF));

    } else if (!fechaInicial.isEmpty()) {

        dateI = Date.from(LocalDateTime.parse(fechaInicial).toInstant(ZoneOffset.UTC));
        query.append("fecha", new Document("$gte", dateI));

    } else if (!fechaFinal.isEmpty()) {
        dateF = Date.from(LocalDateTime.parse(fechaFinal).toInstant(ZoneOffset.UTC));
        query.append("fecha", new Document("$lte", dateF));
    }

    //Lista donde se guardaran las bitacoras a enviar
    List<DBObject> dbo = bitacoras.find().into(new ArrayList<>());
    Map<Integer, List<DBObject>> map = new HashMap<>();

    if (!emocionInicial.isEmpty() && !emocionFinal.isEmpty()) {
        FindIterable<DBObject> iterable = bitacoras.find(query)
                .projection(new Document("fotografias", 0).append("_id", 0))
                .sort(new BasicDBObject("fecha", 1));

        //Recorro las bitacoras
        iterable.forEach(new Block<DBObject>() {
            @Override
            public void apply(final DBObject document) {
                Integer sesionId = Integer.parseInt(document.get("sesionId") + "");

                List<DBObject> list = new ArrayList<>();

                if (!map.containsKey(sesionId)) {
                    list.add(document);
                    map.put(sesionId, list);

                } else {
                    map.get(sesionId).add(document);
                }
            }
        });

        List<DBObject> lista = new ArrayList<>();
        Iterator it = map.keySet().iterator();

        while (it.hasNext()) {
            List<DBObject> l = map.get(it.next());
            String emocionIni = l.get(0).get("emocion").toString().toUpperCase();
            String emocionFin = l.get(l.size() - 1).get("emocion").toString().toUpperCase();

            if (emocionIni.equals(emocionInicial.toUpperCase())
                    && emocionFin.equals(emocionFinal.toUpperCase())) {
                lista.addAll(l);
            }
        }
        dbo = lista;

        //Si solo se busca las bitacoras con la emocin inicial
    } else if (!emocionInicial.isEmpty()) {
        FindIterable<DBObject> iterable = bitacoras.find(query)
                .projection(new Document("fotografias", 0).append("_id", 0))
                .sort(new BasicDBObject("fecha", 1));

        //Recorro las bitacoras
        iterable.forEach(new Block<DBObject>() {
            @Override
            public void apply(final DBObject document) {
                Integer sesionId = Integer.parseInt(document.get("sesionId") + "");

                List<DBObject> list = new ArrayList<>();

                if (!map.containsKey(sesionId)) {
                    list.add(document);
                    map.put(sesionId, list);

                } else {
                    map.get(sesionId).add(document);
                }
            }
        });

        List<DBObject> lista = new ArrayList<>();
        Iterator it = map.keySet().iterator();

        while (it.hasNext()) {
            List<DBObject> l = map.get(it.next());
            String emocionIni = l.get(0).get("emocion").toString().toUpperCase();

            if (emocionIni.equals(emocionInicial.toUpperCase())) {
                lista.addAll(l);
            }
        }
        dbo = lista;

        //Si solo se busca las bitacoras con la emocion final    
    } else if (!emocionFinal.isEmpty()) {
        FindIterable<DBObject> iterable = bitacoras.find(query)
                .projection(new Document("fotografias", 0).append("_id", 0))
                .sort(new BasicDBObject("fecha", 1));

        //Recorro las bitacoras
        iterable.forEach(new Block<DBObject>() {
            @Override
            public void apply(final DBObject document) {
                Integer sesionId = Integer.parseInt(document.get("sesionId") + "");

                List<DBObject> list = new ArrayList<>();

                if (!map.containsKey(sesionId)) {
                    list.add(document);
                    map.put(sesionId, list);

                } else {
                    map.get(sesionId).add(document);
                }
            }
        });

        List<DBObject> lista = new ArrayList<>();
        Iterator it = map.keySet().iterator();

        while (it.hasNext()) {
            List<DBObject> l = map.get(it.next());
            String emocionFin = l.get(l.size() - 1).get("emocion").toString().toUpperCase();
            if (emocionFin.equals(emocionFinal.toUpperCase())) {
                lista.addAll(l);
            }
        }
        dbo = lista;

    } else {
        dbo = bitacoras.find(query).projection(new Document("fotografias", 0).append("_id", 0))
                .sort(new BasicDBObject("fecha", 1)).into(new ArrayList<>());
    }

    return dbo.toString();
}

From source file:Mangement.Menu.java

public void initialize(URL location, ResourceBundle resources) {
    Connector con = new Connector();
    con.connect();//w w  w  .j a v a 2 s  .  c  o  m
    MongoCollection<Document> col = con.getData();

    for (Document doc : col.find()) {
        int index = (int) doc.get("itemId");

        String name = (String) doc.get("Customer Name");
        String _for = (String) doc.get("Company");
        String Billof = (String) doc.get("Price");
        String entry = (String) doc.get("Entry_by");
        DefaultTableModel model;
        model = (DefaultTableModel) jTable1.getModel();
        model.addRow(new Object[] { index, name, _for, Billof, entry });

    }

}

From source file:Mangement.ProClass.java

public void initialize(URL location, ResourceBundle resources) {
    ProductController con = new ProductController();
    con.connect();//from w w w.j  a v  a2  s. c  o  m
    MongoCollection<Document> col = con.getData();

    for (Document doc : col.find()) {
        int index = (int) doc.get("itemId");

        String name = (String) doc.get("Item Name");
        String _for = (String) doc.get("Price");
        String quan = (String) doc.get("Quantity");
        DefaultTableModel model;
        model = (DefaultTableModel) jTable1.getModel();
        model.addRow(new Object[] { index, name, _for, quan });

    }

}