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:com.yahoo.ycsb.db3.MongoDbClient.java

License:Open Source License

/**
 * Perform a range scan for a set of records in the database. Each field/value
 * pair from the result will be stored in a HashMap.
 * //ww w  .j a  v a 2  s  .  c o m
 * @param table
 *          The name of the table
 * @param startkey
 *          The record key of the first record to read.
 * @param recordcount
 *          The number of records to read
 * @param fields
 *          The list of fields to read, or null for all of them
 * @param result
 *          A Vector of HashMaps, where each HashMap is a set field/value
 *          pairs for one record
 * @return Zero on success, a non-zero error code on error. See the {@link DB}
 *         class's description for a discussion of error codes.
 */
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields,
        Vector<HashMap<String, ByteIterator>> result) {
    MongoCursor<Document> cursor = null;
    try {
        MongoCollection<Document> collection = database.getCollection(table);

        Document scanRange = new Document("$gte", startkey);
        Document query = new Document("_id", scanRange);
        Document sort = new Document("_id", INCLUDE);

        FindIterable<Document> findIterable = collection.find(query).sort(sort).limit(recordcount);

        if (fields != null) {
            Document projection = new Document();
            for (String fieldName : fields) {
                projection.put(fieldName, INCLUDE);
            }
            findIterable.projection(projection);
        }

        cursor = findIterable.iterator();

        if (!cursor.hasNext()) {
            System.err.println("Nothing found in scan for key " + startkey);
            return Status.ERROR;
        }

        result.ensureCapacity(recordcount);

        while (cursor.hasNext()) {
            HashMap<String, ByteIterator> resultMap = new HashMap<String, ByteIterator>();

            Document obj = cursor.next();
            fillMap(resultMap, obj);

            result.add(resultMap);
        }

        return Status.OK;
    } catch (Exception e) {
        System.err.println(e.toString());
        return Status.ERROR;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:controller.ProductController.java

public String find(String val) {
    MongoCollection<Document> booklist = db.getCollection("product");
    FindIterable<Document> res = booklist.find(eq("Item Name", val));
    String attr = res.first().get("Price", String.class);
    return attr;//from   w w  w.  ja v  a 2s.c o m
}

From source file:controller.ProductController.java

public String quantity(String val) {
    MongoCollection<Document> booklist = db.getCollection("product");
    FindIterable<Document> res = booklist.find(eq("Item Name", val));
    String attr = res.first().get("Quantity", String.class);
    return attr;/*from   w  ww.  j  a v  a  2s . c o m*/
}

From source file:controller.ProductController.java

public int index(String val) {
    MongoCollection<Document> booklist = db.getCollection("product");
    FindIterable<Document> res = booklist.find(eq("Item Name", val));
    int attr = res.first().get("itemId", Integer.class);
    return attr;//  ww  w.ja va2  s . c o  m
}

From source file:controller.UserController.java

public String find(String val) {
    MongoCollection<Document> mongoCol = db.getCollection("user");
    FindIterable<Document> res = mongoCol.find(eq("Item Name", val));
    String attr = res.first().get("Price", String.class);
    return attr;/* w w w .j a  v  a  2 s .  c  o m*/
}

From source file:controller.UserController.java

public String quantity(String val) {
    MongoCollection<Document> mongoCol = db.getCollection("user");
    FindIterable<Document> res = mongoCol.find(eq("Item Name", val));
    String attr = res.first().get("Quantity", String.class);
    return attr;/*from w w  w.  jav a2 s  .  co  m*/
}

From source file:controller.UserController.java

public int index(String val) {
    MongoCollection<Document> mongoCol = db.getCollection("user");
    FindIterable<Document> res = mongoCol.find(eq("Item Name", val));
    int attr = res.first().get("itemId", Integer.class);
    return attr;/*  w ww  .ja v  a 2 s.c  o  m*/
}

From source file:controller.UserController.java

public boolean search(String text) throws NullPointerException {
    MongoCollection<Document> mongoCol = db.getCollection("user");
    try (MongoCursor<Document> cursor = mongoCol.find(eq("handle", text)).iterator()) {
        return !cursor.hasNext();
    }//from ww w. j  ava 2  s.c o m
}

From source file:controller.UserController.java

public boolean logFind(String val, String pass) {
    boolean cond = false;
    MongoCollection<Document> mongoCol = db.getCollection("user");
    try (MongoCursor<Document> cursor = mongoCol.find(eq("handle", val)).iterator()) {
        if (cursor.hasNext()) {

            FindIterable<Document> res = mongoCol.find(eq("handle", val));
            String attr = res.first().get("password", String.class);
            if (attr.equals(pass)) {
                cond = true;/*from ww  w .j av  a2 s.  co m*/
            }
        }
    }
    return cond;
}

From source file:course.homework.Homework2_3.java

License:Apache License

public static void main(String[] args) {

    MongoClient client = new MongoClient();

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

    Bson filter = Filters.eq("type", "homework");

    Bson sort = Sorts.descending("student_id", "score");

    Set<Object> keys = new HashSet<>();
    collection.find(filter).sort(sort).into(new ArrayList<Document>()).stream().forEach(doc -> {
        if (keys.contains(doc.get("student_id"))) {
            System.out.println("Already exists " + doc.get("student_id") + " = " + doc.get("score"));
            collection.deleteOne(Filters.eq("_id", doc.getObjectId("_id")));
        } else {/*from w w w. j a  va2  s . c o m*/
            System.out.println("Does not exist " + doc.get("student_id") + " = " + doc.get("score"));
            keys.add(doc.get("student_id"));
        }
    });
}