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:ARS.DBManager.java

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

    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("first-name", firstName);

    collection.deleteOne(searchQuery);

    if (findUserByName(firstName) == false) {
        return true;
    } else/*from   w w  w .  j av a 2  s  . com*/
        return false;

}

From source file:booklistmanager.TablefrontController.java

public void delete() {
    Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
    alert.setHeaderText("Are you sure?");
    ButtonType yesbtn = new ButtonType("YES");
    ButtonType nobtn = new ButtonType("NO");

    alert.getButtonTypes().removeAll(alert.getButtonTypes());
    alert.getButtonTypes().addAll(yesbtn, nobtn);

    Optional<ButtonType> res = alert.showAndWait();

    if (res.get() == yesbtn) {
        Connector con = new Connector();
        con.connect();/*from ww w.jav a 2  s .c o m*/
        MongoCollection<Document> col = con.getData();

        ObservableList<Book> booksSelected;
        booksSelected = table.getSelectionModel().getSelectedItems();
        if (booksSelected == null)
            return;
        for (Book b : booksSelected) {
            int index = b.getIndex();
            col.deleteOne(eq("bookid", index));
        }

        changeScene("/fxml/tablefront.fxml");
    }

    else
        return;
}

From source file:co.aurasphere.mongodb.university.classes.m101j.homework23.MainClass.java

License:Open Source License

/**
 * The main method.//from   w w  w .  ja va 2  s  .  c  om
 *
 * @param args
 *            the arguments
 */
public static void main(String[] args) {

    MongoClient client = new MongoClient();
    MongoDatabase numbersDB = client.getDatabase("students");
    MongoCollection<Document> grades = numbersDB.getCollection("grades");

    // Gets all the grades.
    MongoCursor<Document> cursor = grades.find(Filters.eq("type", "homework"))
            .sort(Sorts.ascending("student_id", "score")).iterator();

    Object previousStudentId = null;
    try {
        // Finds the lowest homework score.
        while (cursor.hasNext()) {
            Document entry = cursor.next();

            // If the student_id is different from the previous one, this 
            // means that this is the student's lowest graded homework 
            // (they are sorted by score for each student).
            if (!entry.get("student_id").equals(previousStudentId)) {
                Object id = entry.get("_id");
                grades.deleteOne(Filters.eq("_id", id));

            }

            // The current document ID becomes the new previous one.   
            previousStudentId = entry.get("student_id");
        }

        // Gets the student with the highest average in the class.
        AggregateIterable<Document> results = grades
                .aggregate(Arrays.asList(Aggregates.group("$student_id", Accumulators.avg("average", "$score")),
                        Aggregates.sort(Sorts.descending("average")), Aggregates.limit(1)));

        // There should be only one result. Prints it.
        System.out.println("Solution : " + results.iterator().next().toJson());

    } finally {
        cursor.close();
    }

    client.close();
}

From source file:com.andersen.backendjaxrsproject.EmployeeServices.java

public void deleteEmployee() {

    MongoCollection<Document> employeeCollection = employeeDB.getCollection("EmployeeCollection");
    Bson bson = (Bson) com.mongodb.util.JSON.parse("{}");
    employeeCollection.deleteOne(bson);

}

From source file:com.averageloser.mongodemo.Model.BookDBHelper.java

@Override
public boolean removeDocument(MongoCollection<Document> collection, Book object) {
    Document doc = this.getBookDocument(object);

    collection.deleteOne(doc);

    return false;
}

From source file:com.chadcover.Homework_23.java

License:Apache License

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

    long count = collection.count();
    System.out.println(count);//from   w  w  w .  ja  v a2s.c om

    Bson filter = eq("type", "homework");
    Bson sort = ascending("student_id", "score");
    Bson projection = fields(include("student_id", "score"));

    // better for large datasets
    MongoCursor<Document> result = collection.find(filter).sort(sort).projection(projection).iterator();
    try {
        int lastId = 0;
        int counter = 0;
        while (result.hasNext()) {
            Document cur = result.next();
            int id = (Integer) cur.get("student_id");
            // when moving to new student
            // delete that record, which is the lowest homework score
            if (id != lastId || counter == 0) {
                double grade = (Double) cur.get("score");
                System.out.println("Delete this score: " + grade);
                collection.deleteOne(eq("_id", cur.get("_id")));
            } else {
                // do nothing
            }
            lastId = id;
            counter++;
        }
    } finally {
        result.close();
    }

}

From source file:com.cognifide.aet.vs.metadata.MetadataDAOMongoDBImpl.java

License:Apache License

@Override
public boolean removeSuite(DBKey dbKey, String correlationId, Long version) throws StorageException {
    LOGGER.debug("Removing suite with correlationId {}, version {} from {}.", correlationId, version, dbKey);
    MongoCollection<Document> metadata = getMetadataCollection(dbKey);

    final DeleteResult deleteResult = metadata
            .deleteOne(Filters.and(Filters.eq(CORRELATION_ID_PARAM_NAME, correlationId),
                    Filters.eq(SUITE_VERSION_PARAM_NAME, version)));

    return deleteResult.getDeletedCount() == 1;
}

From source file:com.cognitive.cds.invocation.mongo.IntentMappingDao.java

License:Apache License

public DeleteResult deleteIntent(String intentName) throws JsonProcessingException {
    MongoClient mongo = mongoDbDao.getMongoClient();
    MongoDatabase db = mongo.getDatabase("intent");
    MongoCollection<Document> collection = db.getCollection("cdsintent");

    BasicDBObject query = new BasicDBObject();
    query.append("name", intentName);

    DeleteResult result = collection.deleteOne(query);
    return result;
}

From source file:com.cognitive.cds.invocation.mongo.WorkProductDao.java

License:Apache License

public DeleteResult deleteWorkProduct(String id) throws JsonProcessingException {

    mongoDbDao.setDatabase("work");
    MongoClient mongo = mongoDbDao.getMongoClient();
    MongoDatabase db = mongo.getDatabase("work");
    MongoCollection<Document> collection = db.getCollection("work");

    BasicDBObject query = new BasicDBObject();
    query.append("_id", new ObjectId(id));

    DeleteResult result = collection.deleteOne(query);
    return result;
}

From source file:com.cognitive.cds.invocation.mongo.WorkProductSubscriptionDao.java

License:Apache License

public String deleteWorkProductSubscription(WorkProductSubscription wps) throws JsonProcessingException {

    mongoDbDao.setDatabase("work");
    MongoClient mongo = mongoDbDao.getMongoClient();
    MongoDatabase db = mongo.getDatabase("work");
    MongoCollection<Document> collection = db.getCollection("subscriptions");

    BasicDBObject query = new BasicDBObject();
    query.append("user", wps.getUser());

    DeleteResult result = collection.deleteOne(query);

    return result.toString();
}