Example usage for com.mongodb.client MongoCollection deleteOne

List of usage examples for com.mongodb.client MongoCollection deleteOne

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection deleteOne.

Prototype

DeleteResult deleteOne(Bson filter);

Source Link

Document

Removes at most one document from the collection that matches the given filter.

Usage

From source file:controller.ProductController.java

public void delete(String attr, Object val) {
    MongoCollection<Document> booklist = db.getCollection("product");
    booklist.deleteOne(eq(attr, val));
}

From source file:controller.UserController.java

public void delete(String attr, Object val) {
    MongoCollection<Document> mongoCol = db.getCollection("user");
    mongoCol.deleteOne(eq(attr, val));
}

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 {/*  w w  w . j av  a2  s .  co m*/
            System.out.println("Does not exist " + doc.get("student_id") + " = " + doc.get("score"));
            keys.add(doc.get("student_id"));
        }
    });
}

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   w  w w . j a  v a2s .  c om*/

    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 void deleteFromDb(DBManagerMongo manager) throws Exception {
    MongoCollection<Document> coll = manager.getDb().getCollection("activity");
    DeleteResult result = coll.deleteOne(and(eq("id", getId()), eq("hash", getRemoteHash())));
    if (result.getDeletedCount() != 1)
        throw new Exception("Record was modified or not found");
}

From source file:data.Project.java

License:Open Source License

public void deleteFromDb(DBManagerMongo manager) throws Exception {
    MongoCollection<Document> coll = manager.getDb().getCollection("project");
    DeleteResult result = coll.deleteOne(and(eq("id", getId()), eq("hash", getRemoteHash())));
    if (result.getDeletedCount() != 1)
        throw new Exception("Record was modyfied or not found");

    MongoCollection members = manager.getDb().getCollection("project_member");
    members.deleteMany(eq("project_id", getId()));

    MongoCollection phases = manager.getDb().getCollection("project_phase");
    phases.deleteMany(eq("project_id", getId()));

    MongoCollection activities = manager.getDb().getCollection("activity");
    activities.deleteMany(eq("project_id", getId()));
}

From source file:data.ProjectMember.java

License:Open Source License

public void deleteFromDb(DBManagerMongo manager) throws Exception {
    MongoCollection<Document> coll = manager.getDb().getCollection("project_member");
    DeleteResult result = coll
            .deleteOne(and(and(eq("user_login_name", getUserLoginName()), eq("hash", getRemoteHash())),
                    eq("project_id", getProjectId())));
    if (result.getDeletedCount() != 1)
        throw new Exception("Record was modyfied or not found");

    MongoCollection activity = manager.getDb().getCollection("activity");
    activity.deleteMany(and(eq("project_id", getProjectId()), eq("user_login_name", getUserLoginName())));
}

From source file:data.ProjectPhase.java

License:Open Source License

public void deleteFromDb(DBManagerMongo manager) throws Exception {
    MongoCollection<Document> coll = manager.getDb().getCollection("project_phase");
    DeleteResult result = coll.deleteOne(and(eq("id", getId()), eq("hash", getRemoteHash())));
    if (result.getDeletedCount() != 1)
        throw new Exception("Record was modyfied or not found");

    MongoCollection activities = manager.getDb().getCollection("activity");
    activities.deleteMany(eq("project_phase_id", getId()));
}

From source file:data.User.java

License:Open Source License

public void deleteFromDb(DBManagerMongo manager) throws Exception {
    ArrayList<Project> ownedProjects = Project.getProjectsByUserName(getLoginName(), manager);
    for (Project p : ownedProjects)
        p.deleteFromDb(manager);//from   w ww  .  j ava  2 s . c  o m

    MongoCollection<Document> coll = manager.getDb().getCollection("user");
    DeleteResult result = coll.deleteOne(and(eq("login_name", getLoginName()), eq("hash", getRemoteHash())));
    if (result.getDeletedCount() != 1)
        throw new Exception("Record was modyfied or not found");

    MongoCollection members = manager.getDb().getCollection("project_member");
    members.deleteMany(eq("user_login_name", getLoginName()));

    MongoCollection activities = manager.getDb().getCollection("activity");
    activities.deleteMany(eq("user_login_name", getLoginName()));

    MongoCollection project = manager.getDb().getCollection("project");
    project.deleteMany(eq("user_login_name", getLoginName()));
}

From source file:data.User.java

License:Open Source License

public void deleteFromDB(DBManagerMongo manager) throws Exception {
    ArrayList<Project> ownedProjects = Project.getProjectsByUserName(getLoginName(), manager);
    for (Project p : ownedProjects)
        p.deleteFromDb(manager);//from  w  ww .j  a v  a 2 s  . com

    MongoCollection<Document> coll = manager.getDb().getCollection("user");
    DeleteResult result = coll.deleteOne(and(eq("login_name", getLoginName()), eq("hash", getRemoteHash())));
    if (result.getDeletedCount() != 1)
        throw new Exception("Record was modyfied or not found");

    MongoCollection members = manager.getDb().getCollection("project_member");
    members.deleteMany(eq("user_login_name", getLoginName()));

    MongoCollection activities = manager.getDb().getCollection("activity");
    activities.deleteMany(eq("user_login_name", getLoginName()));

    MongoCollection project = manager.getDb().getCollection("project");
    project.deleteMany(eq("user_login_name", getLoginName()));
}