From the course: MongoDB Java Developer Associate Cert Prep

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Deleting documents in Java applications

Deleting documents in Java applications

- Hello, in this video, you'll learn how to use the deleteOne and deleteMany methods in the filters builder class for a Java application using MongoDB as its backing database. You can delete a single document from a collection by using the deleteOne method from a Mongo collection instance. For brevity, we'll assume that we have already instantiated the Mongo client and Mongo collection instances. The deleteOne method accepts a query filter that matches the document that you want to delete. It returns an instance of a DeleteResult, which contains information about how many documents were deleted. The following code deletes a single document from the accounts collection in the accounts database. This example uses the filters builder class method EQ to match an account belonging to John Doe. Then the deleteOne method is called to delete John doe's account. To confirm it was deleted, We print out the count of the number of deleted documents. If the deleteOne method is called with an empty…

Contents