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:com.mycompany.database.MongodbDataBaseClass.java

public void dududu() {
    System.out.println("not error");
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase database = mongoClient.getDatabase("testdb");
    System.out.println("not error");
    MongoCollection<Document> col = database.getCollection("cars");

    try (MongoCursor<Document> cur = col.find().iterator()) {
        while (cur.hasNext()) {

            Document doc = cur.next();

            List list = new ArrayList(doc.values());
            System.out.print(list.get(1));
            System.out.print(": ");
            System.out.println(list.get(2));
        }//from w w w.  jav a  2s.  c  o  m
    }

    mongoClient.close();
}

From source file:com.mycompany.team2.Game.java

private void ComputeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ComputeButtonActionPerformed
    // TODO add your handling code here:
    if (jComboBox1.getSelectedItem().toString().equals("ADD")) {
        System.out.println("Heelo dropdown");
        num1 = Textbox1.getText();/*from  www  . java2 s.  c  om*/
        num2 = Textbox2.getText();
        number1 = Integer.parseInt(num1);
        number2 = Integer.parseInt(num2);
        int answer = methd.addNum(number1, number2);
        String answerAsString = Integer.toString(answer);
        TextboxAnswer.setText(answerAsString);

        Document doc = new Document();
        doc.put("ADD", answerAsString);
        collx.insertOne(doc);
        System.out.println("Inserted to log");
        /* DBCursor cursor = (DBCursor) collx.find();
         int i = 1;
         while(cursor.hasNext()){
          System.out.println("Inserted Document: " + i);
          System.out.println(cursor.next());
          i++;
         }
        */
    }

    if (jComboBox1.getSelectedItem().toString().equals("SUBTRACT")) {
        System.out.println("Heelo dropdown");
        num1 = Textbox1.getText();
        num2 = Textbox2.getText();
        number1 = Integer.parseInt(num1);
        number2 = Integer.parseInt(num2);
        int answer = methd.subNum(number1, number2);
        String answerAsString = Integer.toString(answer);
        TextboxAnswer.setText(answerAsString);
        try {
            MongoCollection collx = database.getCollection("TeamCity");
            BasicDBObject doc = new BasicDBObject();
            doc.put("SUBTRACT", answerAsString);
            collx.insertOne(doc);
            DBCursor cursor = (DBCursor) collx.find();
            int i = 1;
            while (cursor.hasNext()) {
                System.out.println("Inserted Document: " + i);
                System.out.println(cursor.next());
                i++;
            }
        } catch (Exception e) {
            System.out.println("Catch The answer");
        }
    }

    if (jComboBox1.getSelectedItem().toString().equals("DIVIDE")) {
        System.out.println("Heelo dropdown");
        num1 = Textbox1.getText();
        num2 = Textbox2.getText();
        number1 = Integer.parseInt(num1);
        number2 = Integer.parseInt(num2);
        int answer = methd.divNum(number1, number2);
        String answerAsString = Integer.toString(answer);
        TextboxAnswer.setText(answerAsString);
        try {
            BasicDBObject doc = new BasicDBObject();
            doc.put("DIVIDE", answerAsString);
            collx.insertOne(doc);
            DBCursor cursor = (DBCursor) collx.find();
            int i = 1;
            while (cursor.hasNext()) {
                System.out.println("Inserted Document: " + i);
                System.out.println(cursor.next());
                i++;
            }
        } catch (Exception e) {
            System.out.println("Catch The answer");
        }
    }

    if (jComboBox1.getSelectedItem().toString().equals("MULTIPLY")) {
        System.out.println("Heelo dropdown");
        num1 = Textbox1.getText();
        num2 = Textbox2.getText();
        number1 = Integer.parseInt(num1);
        number2 = Integer.parseInt(num2);
        int answer = methd.mulNum(number1, number2);
        String answerAsString = Integer.toString(answer);
        TextboxAnswer.setText(answerAsString);
        try {
            BasicDBObject doc = new BasicDBObject();
            doc.put("MULTIPLY", answerAsString);
            collx.insertOne(doc);
            DBCursor cursor = (DBCursor) collx.find();
            int i = 1;
            while (cursor.hasNext()) {
                System.out.println("Inserted Document: " + i);
                System.out.println(cursor.next());
                i++;
            }
        } catch (Exception e) {
            System.out.println("Catch The answer");
        }
    }

    if (jComboBox1.getSelectedItem().toString().equals("COUNTRY")) {
        System.out.println("Heelo dropdown");
        num1 = Textbox1.getText();
        num2 = Textbox2.getText();
        number1 = Integer.parseInt(num1);
        number2 = Integer.parseInt(num2);
        int answer = methd.retCountry(number1, number2);
        String answerAsString = methd.getCountry();
        TextboxAnswer.setText(answerAsString);
        try {
            BasicDBObject doc = new BasicDBObject();
            doc.put("COUNTRY", answerAsString);
            collx.insertOne(doc);
            DBCursor cursor = (DBCursor) collx.find();
            int i = 1;
            while (cursor.hasNext()) {
                System.out.println("Inserted Document: " + i);
                System.out.println(cursor.next());
                i++;
            }
        } catch (Exception e) {
            System.out.println("Catch The answer");
        }
    }

}

From source file:com.navercorp.pinpoint.plugin.mongodb.MongoDBBase.java

License:Apache License

public void readData(PluginTestVerifier verifier, MongoCollection<Document> collection,
        Class<?> mongoDatabaseImpl) {
    //read data//from  www.  ja v  a  2 s  . c o  m
    MongoCursor<Document> cursor = collection.find().iterator();

    Method find;
    try {
        find = mongoDatabaseImpl.getDeclaredMethod("find");
    } catch (NoSuchMethodException e) {
        find = null;
    }

    verifier.verifyTrace(event(MONGO_EXECUTE_QUERY, find, null, MONGODB_ADDRESS, null,
            new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_INFO.getName(), "customers"),
            new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_OPTION.getName(), "secondaryPreferred")));

    int resultCount = 0;
    try {
        while (cursor.hasNext()) {
            resultCount++;
            cursor.next();
        }
    } finally {
        cursor.close();
    }
    Assert.assertEquals(2, resultCount);
}

From source file:com.netflix.config.sources.MongoDBConfigurationSource.java

License:Apache License

/**
 * Returns a <code>Map<String, Object></code> of properties stored in the
 * database.//from   ww  w. j  av a 2  s  . co  m
 *
 * @throws Exception
 */
synchronized Map<String, Object> load() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();

    try {
        final MongoDatabase database = mongoClient.getDatabase(databaseName);
        final MongoCollection<Document> collection = database.getCollection(collectionName);
        final FindIterable<Document> documents = collection.find();
        for (Document document : documents) {
            final Object id = document.get("_id");
            if (id != null && document.containsKey("value")) {
                final Object value = document.get("value");
                map.put(id.toString(), value);
            }
        }

    } catch (Exception e) {
        log.error("Failed to load archaius settings from MongoDB.", e);
        throw e;
    }
    return map;
}

From source file:com.redhat.thermostat.gateway.common.mongodb.executor.MongoExecutor.java

License:Open Source License

public MongoDataResultContainer execGetRequest(MongoCollection<Document> collection, Integer limit,
        Integer offset, String sort, List<String> queries, String includes, String excludes,
        Set<String> realms) {
    FindIterable<Document> documents = collection.find();
    MongoDataResultContainer queryDataContainer = new MongoDataResultContainer();

    Bson query = MongoRequestFilters.buildQuery(queries, realms);
    documents = documents.filter(query);
    long count = collection.count(query);
    queryDataContainer.setGetReqCount(count);
    queryDataContainer.setRemainingNumQueryDocuments((int) (count - (limit + offset)));
    documents = buildProjection(documents, includes, excludes);
    final Bson sortObject = MongoSortFilters.createSortObject(sort);
    documents = documents.sort(sortObject).limit(limit).skip(offset).batchSize(limit)
            .cursorType(CursorType.NonTailable);
    queryDataContainer.setQueryDataResult(documents);

    return queryDataContainer;
}

From source file:com.redhat.thermostat.gateway.common.mongodb.MongoStorageHandler.java

License:Open Source License

public String getMany(MongoCollection<Document> collection, Bson query, Integer limit, Integer offset,
        String sort, String includes, String excludes) {
    FindIterable<Document> documents = query == null ? collection.find() : collection.find(query);
    documents = buildProjection(documents, includes, excludes);
    final Bson sortObject = MongoSortFilters.createSortObject(sort);
    documents = documents.sort(sortObject).limit(limit).skip(offset).batchSize(limit)
            .cursorType(CursorType.NonTailable);
    return mongoResponseBuilder.addQueryDocuments(documents).build();
}

From source file:com.shampan.model.GeneralModel.java

public List<CountriesDAO> getAllCountries() {
    DBConnection.getInstance().getConnection();
    MongoCollection<CountriesDAO> mongoCollection = DBConnection.getInstance().getConnection()
            .getCollection("countries", CountriesDAO.class);
    MongoCursor<CountriesDAO> cursorCountryList = mongoCollection.find().iterator();
    List<CountriesDAO> countryList = IteratorUtils.toList(cursorCountryList);
    return countryList;
}

From source file:com.shampan.model.GeneralModel.java

public List<ReligionsDAO> getAllReligions() {
    MongoDatabase db = DBConnection.getInstance().getConnection();
    MongoCollection<ReligionsDAO> mongoCollection = DBConnection.getInstance().getConnection()
            .getCollection("religions", ReligionsDAO.class);
    MongoCursor<ReligionsDAO> CursorReligionList = mongoCollection.find().iterator();
    List<ReligionsDAO> religionList = IteratorUtils.toList(CursorReligionList);
    return religionList;

}

From source file:com.shampan.model.PageModel.java

public List<PageCategoryDAO> getCategories() {
    MongoCollection<PageCategoryDAO> mongoCollection = DBConnection.getInstance().getConnection()
            .getCollection(Collections.PAGECATEGORIES.toString(), PageCategoryDAO.class);
    MongoCursor<PageCategoryDAO> cursorCategoryList = mongoCollection.find().iterator();
    List<PageCategoryDAO> categoryList = IteratorUtils.toList(cursorCategoryList);
    return categoryList;

}

From source file:com.shampan.model.PageModel.java

public List<PageSubCategoryDAO> getSubCategories() {
    MongoCollection<PageSubCategoryDAO> mongoCollection = DBConnection.getInstance().getConnection()
            .getCollection(Collections.PAGESUBCATEGORIES.toString(), PageSubCategoryDAO.class);
    MongoCursor<PageSubCategoryDAO> cursorCategoryList = mongoCollection.find().iterator();
    List<PageSubCategoryDAO> subCategoryList = IteratorUtils.toList(cursorCategoryList);
    return subCategoryList;

}