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(ClientSession clientSession);

Source Link

Document

Finds all documents in the collection.

Usage

From source file:ARS.DBManager.java

public ArrayList findFlight(String departure, String arrival) {

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("ars");
    MongoCollection collection = db.getCollection("users"); //Choosing the collection to insert

    BasicDBObject query = new BasicDBObject();
    query.put("departure", departure);
    query.put("arrival", arrival);
    MongoCursor cursor = collection.find(query).iterator();

    ArrayList<Flight> flightList = new ArrayList<>();
    //Counter gives one if it's give the result
    while (cursor.hasNext()) {
        flightList.add((Flight) cursor.next());
    }/*w w w.j  a va2 s.c o  m*/
    return flightList;

}

From source file:at.grahsl.kafka.connect.mongodb.end2end.MinimumViableIT.java

License:Apache License

@Test
@DisplayName("when producing kafka records then verify saved mongodb documents")
public void produceKafkaRecordsAndVerifyMongoDbDocuments() {

    int numTestRecords = 100;
    for (int tid = 0; tid < numTestRecords; tid++) {
        TweetMsg tweet = TweetMsg.newBuilder().setId$1(tid)
                .setText("test tweet " + (tid)
                        + ": end2end testing apache kafka <-> mongodb sink connector is fun!")
                .setHashtags(Arrays.asList(new String[] { "t" + tid, "kafka", "mongodb", "testing" })).build();

        ProducerRecord<String, TweetMsg> record = new ProducerRecord<>("e2e-test-topic", tweet);
        System.out.println(LocalDateTime.now() + " producer sending -> " + tweet.toString());

        PRODUCER.send(record, (RecordMetadata r, Exception exc) -> {
            assertNull(exc, () -> "unexpected error while sending: " + tweet + " | exc: " + exc.getMessage());
        });/*from   w w  w.j a v  a 2  s.  c om*/
    }

    deferExecutionToWaitForDataPropagation(Duration.ofSeconds(10),
            "giving the processing some time to propagate the data");

    for (int tid = 0; tid < numTestRecords; tid++) {
        MongoCollection<Document> col = MONGO_DATABASE.getCollection("e2e-test-collection");
        Document found = col.find(eq(tid)).first();
        //NOTE: this only verifies whether a document with the expected _id field
        //exists everything else isn't particularly interesting during E2E scenarios
        //but instead rigorously checked within a bunch of specific unit tests
        assertNotNull(found, "document having _id=" + tid + " is expected to be found");
        System.out.println("found = " + found);
    }

}

From source file:bariopendatalab.db.DBAccess.java

public String findByType(String type, int offset, int size) throws Exception {
    MongoDatabase database = client.getDatabase(DBNAME);
    MongoCollection<Document> collection = database.getCollection(COLLNAME);
    FindIterable<Document> documents = collection.find(new Document().append("type", type)).skip(offset)
            .limit(size);/*from  w w w . j av a2s.  c  om*/
    List<Document> list = new ArrayList<>();
    for (Document doc : documents) {
        list.add(doc);
    }
    Document response = new Document();
    response.append("size", list.size());
    response.append("results", list);
    return response.toJson();
}

From source file:bariopendatalab.db.DBAccess.java

public String poiByMunicipio(int id) throws Exception {
    MongoDatabase database = client.getDatabase(DBNAME);
    MongoCollection<Document> collMunicipi = database.getCollection(COLLMUNICIPI);
    Document municipioDoc = collMunicipi.find(new Document().append("properties.OBJECTID", id)).first();
    MongoCollection<Document> collection = database.getCollection(COLLNAME);
    FindIterable<Document> documents = collection.find(new Document("geometry",
            new Document("$geoWithin", new Document("$geometry", municipioDoc.get("geometry")))));
    List<Document> list = new ArrayList<>();
    for (Document doc : documents) {
        list.add(doc);/*from  ww  w.j a  v a 2s .co m*/
    }
    Document response = new Document();
    response.append("size", list.size());
    response.append("results", list);
    return response.toJson();
}

From source file:bariopendatalab.db.DBAccess.java

public List<Document> poiListByMunicipio(int id) throws Exception {
    MongoDatabase database = client.getDatabase(DBNAME);
    MongoCollection<Document> collMunicipi = database.getCollection(COLLMUNICIPI);
    Document municipioDoc = collMunicipi.find(new Document().append("properties.OBJECTID", id)).first();
    MongoCollection<Document> collection = database.getCollection(COLLNAME);
    FindIterable<Document> documents = collection.find(new Document("geometry",
            new Document("$geoWithin", new Document("$geometry", municipioDoc.get("geometry")))));
    List<Document> list = new ArrayList<>();
    for (Document doc : documents) {
        list.add(doc);//  w  w  w.  j a  va  2s . co  m
    }
    return list;
}

From source file:be.nille.blog.mongo.post.MPostAccess.java

@Override
public Author getAuthor() {
    MongoCollection authorCollection = database.getCollection("author");
    ObjectId authorObjectId = (ObjectId) document.get("author");
    FindIterable<Document> iterable = authorCollection.find(Filters.eq("_id", authorObjectId));
    Document first = iterable.first();
    if (first != null) {
        return new Author(new MAuthorAccess(first));
    }//w  w w.  j a v  a2s. c  om
    return null;
}

From source file:be.nille.blog.mongo.post.MPostAccess.java

@Override
public Category getCategory() {
    MongoCollection categoryCollection = database.getCollection("category");
    ObjectId categoryObjectId = (ObjectId) document.get("category");
    FindIterable<Document> iterable = categoryCollection.find(Filters.eq("_id", categoryObjectId));
    Document first = iterable.first();
    if (first != null) {
        return new Category(new MCategoryAccess(first));
    }//from w  w w.j a va2  s .com
    return null;
}

From source file:booklistmanager.DetailsController.java

/**
 * Initializes the controller class.//ww  w  .  j ava2 s  .  c  o m
 */
@Override
public void initialize(URL url, ResourceBundle rb) {

    Connector con = new Connector();
    con.connect();
    MongoCollection<Document> col = con.getData();

    Document index = col.find(eq("bookid", ind)).first();

    System.out.println(index.toJson());
    nameshower.setText("Name : " + (String) index.get("Name"));
    forshower.setText("For : " + (String) index.get("For"));
    if (index.containsKey("Author(s)"))
        authorshower.setText("Author(s) : " + (String) index.get("Author(s)"));
    else
        authorshower.setVisible(false);
    if (index.containsKey("Publish date"))
        publishshower.setText("Publish date : " + (String) index.get("Publish date"));
    else
        container.getChildren().remove(publishshower);
    if (index.containsKey("Edition"))
        editionshower.setText("Edition : " + (String) index.get("Edition"));
    else
        container.getChildren().remove(editionshower);

}

From source file:booklistmanager.TablefrontController.java

public void search() {
    if (searchField.getText().isEmpty()) {
        errorhandle("Search Field is empty");
        return;/*ww  w  . j  a  va2  s.c o m*/
    }

    String text = searchField.getText();
    searchField.clear();

    Connector con = new Connector();
    con.connect();
    MongoCollection<Document> col = con.getData();

    BasicDBObject q = new BasicDBObject();
    q.put("Name", Pattern.compile("^" + text, Pattern.UNIX_LINES));

    forChooser.getItems().add("Search Result for " + text);
    forChooser.getSelectionModel().clearAndSelect(forChooser.getItems().size() - 1);

    table.setItems(getBook(col.find(q)));
}

From source file:booklistmanager.TablefrontController.java

public ObservableList<Book> getBook(String s) {
    ObservableList<Book> books = FXCollections.observableArrayList();

    Connector con = new Connector();
    con.connect();//from   www  .j  a  v  a  2s  . c  o  m
    MongoCollection<Document> col = con.getData();
    int sl = 1;
    for (Document d : col.find(eq("For", s))) {
        Book b = new Book(d, sl++);
        books.add(b);
    }
    return books;
}