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:course.homework.week2.RemoveLowest.java

License:Apache License

public static void main(final String[] args) {
    MongoClient client = new MongoClient();
    MongoDatabase numbersDB = client.getDatabase("students");
    MongoCollection<Document> grades = numbersDB.getCollection("grades");

    MongoCursor<Document> cursor = grades.find(eq("type", "homework")).sort(ascending("student_id", "score"))
            .iterator();/*from  ww  w.j  a  va2s . c  o  m*/

    Object studentId = -1;
    try {
        while (cursor.hasNext()) {
            Document entry = cursor.next();
            if (!entry.get("student_id").equals(studentId)) {
                System.out.println("Removing: " + entry);
                Object id = entry.get("_id");
                grades.deleteOne(eq("_id", id));

            }
            studentId = entry.get("student_id");
        }
    } finally {
        cursor.close();
    }
}

From source file:data.Activity.java

License:Open Source License

public static Activity getByPrimaryKey(int id, DBManagerMongo manager) throws Exception {
    MongoCollection<Document> coll = manager.getDb().getCollection("activity");
    FindIterable<Document> doc = coll.find(eq("id", id));
    MongoCursor<Document> cursor = doc.iterator();
    if (!cursor.hasNext())
        throw new Exception("no such record");

    return extractActivity(cursor.next(), manager);
}

From source file:data.Activity.java

License:Open Source License

public static void getParticipatingProjectsAndWorkloadSince(String loginName, ZonedDateTime since,
        ArrayList<Project> projects, ArrayList<Duration> durations, DBManagerMongo manager) throws Exception {
    MongoCollection coll = manager.getDb().getCollection("activity");
    ArrayList<ProjectMember> owned = ProjectMember.getInvolvedProjects(loginName, manager);
    owned.addAll(ProjectMember.getOwnedProject(loginName, manager));
    for (ProjectMember m : owned) {
        projects.add(m.getProject());/*  ww  w  . ja va2s  .  c  om*/
        FindIterable<Document> result = coll.find(and(eq("user_login_name", loginName),
                gte("start_time", java.util.Date.from(since.toInstant())), eq("project_id", m.getProjectId())));
        Duration sum = Duration.ZERO;
        for (Document doc : result) {
            java.util.Date startDate = doc.getDate("start_time");
            Date endDate = doc.getDate("end_time");
            Duration duration = Duration.between(startDate.toInstant(), endDate.toInstant());
            sum = sum.plus(duration);
        }
        durations.add(sum);
    }
}

From source file:data.Activity.java

License:Open Source License

public static void getPhasesAndWorkloadSince(String loginName, int projectId, ZonedDateTime since,
        ArrayList<ProjectPhase> phases, ArrayList<Duration> durations, DBManagerMongo manager)
        throws Exception {
    Date sinceDate = Date.from(since.toInstant());
    Bson match = and(eq("user_login_name", loginName), gt("start_time", sinceDate),
            eq("project_id", projectId));

    MongoCollection coll = manager.getDb().getCollection("activity");
    FindIterable<Document> result = coll.find(match);

    for (Document doc : result) {
        int projectPhaseId = doc.getInteger("project_phase_id");
        ProjectPhase projectPhase = ProjectPhase.getByPrimaryKey(projectPhaseId, manager);
        phases.add(projectPhase);// w  w  w . ja va2s .  c om

        Date startDate = doc.getDate("start_time");
        Date endDate = doc.getDate("end_time");
        Duration duration = Duration.between(startDate.toInstant(), endDate.toInstant());
        durations.add(duration);
    }
}

From source file:data.Activity.java

License:Open Source License

public static ArrayList<Activity> getActivitiesForPhaseSince(String loginName, int phaseId, ZonedDateTime since,
        DBManagerMongo manager) throws Exception {
    ArrayList<Activity> list = new ArrayList<>();
    Date sinceDate = Date.from(since.toInstant());
    MongoCollection coll = manager.getDb().getCollection("activity");
    FindIterable<Document> result = coll.find(and(eq("user_login_name", loginName),
            gte("start_time", sinceDate), eq("project_phase_id", phaseId)));

    for (Document doc : result)
        list.add(extractActivity(doc, manager));
    return list;/*from  www.ja  v  a2  s  . co  m*/
}

From source file:data.Activity.java

License:Open Source License

public static ArrayList<Activity> getActivitiesByUserForPhaseSince(String loginName, int phaseId,
        ZonedDateTime since, DBManagerMongo manager) throws Exception {
    ArrayList<Activity> list = new ArrayList<>();
    Date sinceDate = Date.from(since.toInstant());
    MongoCollection coll = manager.getDb().getCollection("activity");
    FindIterable<Document> result = coll
            .find(and(loginName.isEmpty() ? not(eq("user_login_name", "")) : eq("user_login_name", loginName),
                    gte("start_time", sinceDate), eq("project_phase_id", phaseId)));

    for (Document doc : result)
        list.add(extractActivity(doc, manager));
    return list;//from w  w  w. ja va  2  s  . co m
}

From source file:data.Activity.java

License:Open Source License

public static void getOwnedProjectsAndWorkloadSince(String loginName, ZonedDateTime since,
        ArrayList<Project> projects, ArrayList<Duration> durations, DBManagerMongo manager) throws Exception {
    MongoCollection coll = manager.getDb().getCollection("activity");

    ArrayList<ProjectMember> owned = ProjectMember.getOwnedProject(loginName, manager);
    for (ProjectMember m : owned) {
        projects.add(m.getProject());/*ww  w .  j ava  2  s  .  c  o m*/
        FindIterable<Document> result = coll.find(and(eq("user_login_name", loginName),
                gte("start_time", Date.from(since.toInstant())), eq("project_id", m.getProjectId())));
        Duration sum = Duration.ZERO;
        for (Document doc : result) {
            Date startDate = doc.getDate("start_time");
            Date endDate = doc.getDate("end_time");
            Duration duration = Duration.between(startDate.toInstant(), endDate.toInstant());
            sum = sum.plus(duration);
        }
        durations.add(sum);
    }
}

From source file:data.Activity.java

License:Open Source License

public static void getPhasesAndWorkloadForUserSince(String loginName, int projectId, ZonedDateTime since,
        ArrayList<ProjectPhase> phases, ArrayList<Duration> durations, DBManagerMongo manager)
        throws Exception {
    Date sinceDate = Date.from(since.toInstant());
    Bson match = and(loginName.isEmpty() ? not(eq("user_login_name", "")) : eq("user_login_name", loginName),
            gt("start_time", sinceDate), eq("project_id", projectId));

    MongoCollection coll = manager.getDb().getCollection("activity");
    FindIterable<Document> result = coll.find(match);

    for (Document doc : result) {
        int projectPhaseId = doc.getInteger("project_phase_id");
        ProjectPhase projectPhase = ProjectPhase.getByPrimaryKey(projectPhaseId, manager);
        phases.add(projectPhase);// w w w .  jav a 2  s.c o m

        Date startDate = doc.getDate("start_time");
        Date endDate = doc.getDate("end_time");
        Duration duration = Duration.between(startDate.toInstant(), endDate.toInstant());
        durations.add(duration);
    }
}

From source file:data.Project.java

License:Open Source License

public static Project getByPrimaryKey(int projectId, DBManagerMongo manager) throws Exception {
    MongoCollection<Document> coll = manager.getDb().getCollection("project");
    FindIterable<Document> doc = coll.find(eq("id", projectId));
    MongoCursor<Document> cursor = doc.iterator();
    if (!cursor.hasNext())
        throw new Exception("no such record");

    return extractProject(cursor.next());
}

From source file:data.Project.java

License:Open Source License

public static String getDescriptionByProjectName(String projectName, DBManagerMongo manager) throws Exception {
    MongoCollection<Document> coll = manager.getDb().getCollection("project");
    FindIterable<Document> doc = coll.find(eq("name", projectName));
    MongoCursor<Document> cursor = doc.iterator();
    if (!cursor.hasNext())
        throw new Exception("no such record");

    return cursor.next().getString("description");
}