From the course: MongoDB Node.js Developer Associate Cert Prep

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Deleting documents in Node.js applications

Deleting documents in Node.js applications

- Hello, in this video you will learn how to use the deleteOne and deleteMany methods for a Node.js application with MongoDB. We can delete a single document from a collection by using the deleteOne method. The deleteOne method accepts a query filter that matches the document you want to delete. It returns a delete result, which contains information about how many documents were deleted. The following code deletes a single document from the accountsCollection in the bank database. The example uses a filter document called documentToDelete to match an account with the _id value. Then we pass the filter documentToDelete to the deleteOne method to delete the account. We confirm that the document was deleted by checking the result message. If the deleteOne method is called with an empty query filter, then it matches and deletes the first document in the collection. Multiple documents can be deleted from a collection…

Contents