Lesson 5: Deleting Documents in Python Applications / Learn

Video Transcript (English)

Hello!

In this video, you will learn how to use the delete_one() and delete_many() methods to delete documents in MongoDB by using the PyMongo driver.

We can delete a single document from a collection by using the delete_one() method.

The delete_one() method accepts a query filter that matches the document that we want to delete.

Let’s go through an example.

We’ll work with a database named `bank` that contains an `accounts` collection.

Let’s say that we want to delete the document from the `accounts` collection that matches this _id value.

It’s appropriate to use delete_one() for this query because there can be only one document in a collection that matches any given `_id` value.

We’ve already started a Python file and connected to our Atlas cluster with MongoClient.

We have a reference to the `bank` database and a reference to the `accounts` collection.

First, we create the filter that matches the _id value of the document that we want to delete. We assign the filter to a variable named document_to_delete.

To work with ObjectIds in PyMongo, we need to import the ObjectId class from PyMongo’s bson package. We add this statement to our imports.

Now we’re ready to write the expression that deletes the target document.

We call the `delete_one` method on the accounts_collection object.

Then we pass in the filter argument, document_to_delete.

delete_one() returns an instance of DeleteResult, which allows us to access information about how many documents were deleted.

We assign the result of the delete operation to a variable named 'result'.

Then we add a statement that uses the delete result to print the count of documents that were deleted.

We’ve also added queries for the document before and after the delete operation as an additional confirmation.

Finally, we close the client.

Now let’s run the code. The output shows the document before the delete, and confirms that it was deleted.

Note that if the delete_one() method is called with an empty query filter document, then it matches and deletes the first document in the collection.

To delete multiple documents from a collection in a single operation, we use the delete_many() method.

The delete_many() method accepts a query filter that matches the documents to be deleted.

Let’s go through an example that uses delete_many().

Suppose we want to delete all accounts with a balance less than $2000.

We’ve started a new Python file, connected to our Atlas cluster with MongoClient, and created references to the database and collection.

First, we create the query filter for all accounts with a balance of less than 2000. We assign the filter to a variable named documents_to_delete.

Now we’re ready to write the expression that deletes all documents with a balance less than 2000.

We call the `delete_many` method on the collection object.

We pass in the filter argument, which is the documents_to_delete variable.

Note that if we call the delete_many() method with an empty query filter document, then all documents in the collection will be deleted.

delete_many() returns a result object, which enables the retrieval of information such as the number of documents that were deleted.

We assign the result of this delete operation to a variable named 'result'.

Then we add a statement to print the number of documents that were deleted by using the result object.

We’ve also added queries for a sample document before and after the delete as an additional confirmation.

Let’s run the code!

The output shows a sample document before the delete, and confirms that 13 documents matching our query were deleted.

Well done!

In this video, you learned how to delete documents from a MongoDB collection by using the delete_one() and delete_many() methods with query filters.

Don’t forget that using delete_many() with an empty query filter will delete all documents in the specified collection.