How to Delete More Than One Document in Python


In this example we will show how to delete more than one document using the delete_many() method in Python MongoDB.

Source Code

from pymongo import MongoClient

client = MongoClient("localhost", 27017)
db = client["mydatabase"]
collection = db["us_state"]

query = {"Capital": {"$regex": "^D"}}

a = collection.delete_many(query)

print(a.deleted_count, "documents have been deleted")

Output:

2 documents have been deleted
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments