Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Mongodb - Delete_one()
Next article icon

InMongoDB, deletion is key to efficient data management. PyMongo offers methods to delete a single document, multiple documents, all documents in a collection or the entire collection itself. Here's a quick overview of these techniques:

1. Delete a single document: Use delete_one() to remove only the first document that matches the filter.

res = my_collection.delete_one({"name": "Mr.Geek"})

To see the number of documents deleted :

print(res.deleted_count)

2. Delete Multiple Documents:Use delete_many() to remove all documents that match a specific condition.

res = my_collection.delete_many({"name": "Mr.Geek"})

To see the number of documents deleted :

print(res.deleted_count)

3. Delete All Documents in a Collection: Using delete_many({}): This deletes all documents in the collection but keeps the collection and its indexes.

res = my_collection.delete_many({})

To see the number of documents deleted :

print(res.deleted_count)

4. Drop the entire collection: If you want to remove all documents along with the collection and its indexes, use drop().

db.my_collection.drop()

Examples

Example 1: Delete a single document

Python
frompymongoimportMongoClientc=MongoClient("mongodb://localhost:27017/")db=c["my_database"]col=db["my_collection"]col.insert_many([{"name":"Mr.Geek","role":"Developer"},{"name":"Mr.Geek","role":"Designer"}])res=col.delete_one({"name":"Mr.Geek"})print(res.deleted_count)

Output

1

Explanation:

  • delete_one()deletes only the first document that matches thefilter { "name": "Mr.Geek" }. Even if multiple documents match, only one is removed.
  • deleted_count shows how many documents were deleted (expected: 1).

Example 2: Delete multiple documents

Python
frompymongoimportMongoClientc=MongoClient("mongodb://localhost:27017/")db=c["my_database"]col=db["my_collection"]col.insert_many([{"name":"Mr.Geek","role":"Developer"},{"name":"Mr.Geek","role":"Tester"},{"name":"Mr.Geek","role":"Manager"}])res=col.delete_many({"name":"Mr.Geek"})print(res.deleted_count)

Output

4

Explanation:

  • delete_many() deletes all documents matching the filter { "name": "Mr.Geek" }.
  • deleted_count shows how many documents were deleted.

Example 3: Delete all documents in a collection

Python
frompymongoimportMongoClientc=MongoClient("mongodb://localhost:27017/")db=c["my_database"]col=db["my_collection"]col.insert_many([{"product":"Laptop"},{"product":"Phone"},{"product":"Tablet"}])res=col.delete_many({})print(res.deleted_count)

Output

3

Explanation:

  • Passing an emptyfilter {} to delete_many() means “match all documents”. This will remove every document in the collection.
  • deleted_count will show the total deleted (expected: 3).

Example 4: Drop the entire collection

Python
frompymongoimportMongoClientc=MongoClient("mongodb://localhost:27017/")db=c["my_database"]col=db["my_collection"]col.insert_many([{"product":"Laptop"},{"product":"Phone"},{"product":"Tablet"}])db.my_collection.drop()print("Collection dropped successfully.")

Output

Collection dropped successfully.

Explanation:

  • .drop()deletes the entire collection, including all documents and indexes.
  • db.my_collection.drop()and col.drop() are functionally identical.

Related articles


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp