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:Dao.AccessDataNOSQL.java

License:Open Source License

/**
 * Mtodo para cargar en memoria los usuarios
 *//*from   w ww .  jav a  2  s.  c  om*/
@Override
public void cargarUserDAO() {
    MongoCollection<Document> collec = consultaBD("nosql", "ratings");
    MongoCursor<Document> cursor = collec.find().iterator();

    while (cursor.hasNext()) {
        try {
            String stringjson = cursor.next().toJson();
            JSONObject obj1 = new JSONObject(stringjson);

            String userId = obj1.getString("userId");
            User u = new User();
            //Integer.parseInt(userId);
            u.setUserId(Integer.parseInt(userId));
            getUserDAO().add(u);

        } catch (JSONException ex) {
            Logger.getLogger(AccessDataNOSQL.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    cursor.close();
}

From source file:dao.DaoDocumentos.java

public List<Compra> buscarCompras() {
    MongoClient client = new MongoClient("localhost", 27017);
    MongoDatabase dataBase = client.getDatabase("bdnc-loja");
    MongoCollection<Document> collection = dataBase.getCollection("vendas");
    List<Compra> compras = new ArrayList<>();
    MongoCursor<Document> cursor = collection.find().iterator();
    while (cursor.hasNext()) {
        Compra compra = new Compra();
        compras.add(compra.convertFromDocument(cursor.next()));
    }//from w  w w.  ja v a 2 s .  co  m
    client.close();
    return compras;
}

From source file:data.Activity.java

License:Open Source License

public static List<Activity> getAll(DBManagerMongo manager) throws Exception {
    ArrayList<Activity> list = new ArrayList<>();
    MongoCollection<Document> coll = manager.getDb().getCollection("activity");
    FindIterable<Document> doc = coll.find();
    MongoCursor<Document> cursor = doc.iterator();
    while (cursor.hasNext())
        list.add(extractActivity(cursor.next(), manager));

    return list;/*ww w  .  jav  a2  s . com*/
}

From source file:data.Project.java

License:Open Source License

public static List<Project> getAll(DBManagerMongo manager) throws Exception {
    ArrayList<Project> list = new ArrayList<>();
    MongoCollection<Document> coll = manager.getDb().getCollection("project");
    FindIterable<Document> doc = coll.find();
    MongoCursor<Document> cursor = doc.iterator();
    while (cursor.hasNext())
        list.add(extractProject(cursor.next()));

    return list;//from   ww w  .  ja v a2  s.  c o  m
}

From source file:data.ProjectMember.java

License:Open Source License

public static List<ProjectMember> getAll(DBManagerMongo manager) throws Exception {
    ArrayList<ProjectMember> list = new ArrayList<>();
    MongoCollection<Document> coll = manager.getDb().getCollection("project_member");
    FindIterable<Document> doc = coll.find();
    MongoCursor<Document> cursor = doc.iterator();
    while (cursor.hasNext())
        list.add(extractMember(cursor.next(), manager));

    return list;//from  w w w .j a  va  2s .  co  m
}

From source file:data.ProjectPhase.java

License:Open Source License

public static List<ProjectPhase> getAll(DBManagerMongo manager) throws Exception {
    ArrayList<ProjectPhase> list = new ArrayList<>();
    MongoCollection<Document> coll = manager.getDb().getCollection("project_phase");
    FindIterable<Document> doc = coll.find();
    MongoCursor<Document> cursor = doc.iterator();
    while (cursor.hasNext())
        list.add(extractPhase(cursor.next(), manager));

    return list;//www .  java 2s  .co  m
}

From source file:data.User.java

License:Open Source License

public static List<User> getAll(DBManagerMongo manager) throws Exception {
    ArrayList<User> list = new ArrayList<>();
    MongoCollection<Document> coll = manager.getDb().getCollection("user");
    FindIterable<Document> doc = coll.find();
    MongoCursor<Document> cursor = doc.iterator();
    while (cursor.hasNext())
        list.add(extractUser(cursor.next()));

    return list;/*  w  w  w.ja v  a2 s.c  o m*/
}

From source file:database.BFIdataTable.java

public int getIndex() {
    Document doc = new Document();
    MongoCollection<Document> col = db.getCollection(col_name);
    if (col.count() <= 0)
        return 1;
    for (Document d : col.find())
        doc = d;/* w ww. java 2s .  c o m*/
    int j = (int) doc.get("dataid");
    return j + 1;
}

From source file:DS.Model.java

public List<Restaurant> getRestaurants() {
    MongoCollection<Document> collection = db.getCollection("restaurants");
    MongoCursor<Document> cursor = collection.find().iterator();

    resList = new ArrayList<Restaurant>();
    try {// w w w  .  j av a 2s . com
        //Iterate through all the returned restaurants and store the information into the resList list
        while (cursor.hasNext()) {
            String json = cursor.next().toJson();
            JsonParser parser = new JsonParser();
            JsonElement jsonTree = parser.parse(json);
            if (jsonTree.isJsonObject()) {
                Restaurant r = new Restaurant();
                JsonObject j = jsonTree.getAsJsonObject();
                r.setName(j.get("name").getAsString());
                r.setLocation(j.get("address").getAsString());
                r.setRating(Double.parseDouble(j.get("rating").getAsString()));
                resList.add(r);
            }
        }
    } finally {
        cursor.close();
    }

    return resList;
}

From source file:DS.Model.java

public List<Request> getRequests() {
    MongoCollection<Document> collection = db.getCollection("requests");
    MongoCursor<Document> cursor = collection.find().iterator();

    reqList = new ArrayList<Request>();
    try {/*from w  w w .j a v a2s . c o  m*/
        //Iterate through all the requests to retrieve the information and add to the reqList list
        while (cursor.hasNext()) {
            String json = cursor.next().toJson();
            JsonParser parser = new JsonParser();
            JsonElement jsonTree = parser.parse(json);
            if (jsonTree.isJsonObject()) {
                Request r = new Request();
                JsonObject j = jsonTree.getAsJsonObject();
                String term = j.get("term").getAsString();
                String location = j.get("location").getAsString();
                r.setLocation(location);
                r.setTerm(term);
                r.setTimestamp(j.get("time").getAsString());
                r.setPhone(j.get("phone").getAsString());
                r.setDelay(Double.parseDouble(j.get("delay").getAsString()));
                reqList.add(r);

                //Add the term to the termCount map, used to find which term is the most popular
                //Check if the term already exists.  IF yes, increment by one, else add as new term
                if (termCount.containsKey(term)) {
                    int count = termCount.get(term);
                    termCount.put(term, count + 1);
                } else {
                    termCount.put(term, 0);
                }

                //Add the location to the locCount map, used to find which term is the most popular
                //Check if the location already exists.  IF yes, increment by one, else add as new loc
                if (locCount.containsKey(location)) {
                    int count = locCount.get(location);
                    locCount.put(location, count + 1);
                } else {
                    locCount.put(location, 0);
                }
            }
        }
    } finally {
        cursor.close();
    }

    return reqList;
}