- Python MongoDB - Discussion
- Python MongoDB - Useful Resources
- Python MongoDB - Quick Guide
- Python MongoDB - Limit
- Python MongoDB - Update
- Python MongoDB - Drop Collection
- Python MongoDB - Delete Document
- Python MongoDB - Sort
- Python MongoDB - Query
- Python MongoDB - Find
- Python MongoDB - Insert Document
- Python MongoDB - Create Collection
- Python MongoDB - Create Database
- Python MongoDB - Introduction
- Python MongoDB - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python MongoDB - Delete Document
You can delete documents in a collection using the remove() method of MongoDB. This method accepts two optional parameters −
Deletion criteria specifying the condition to delete documents.
Just one, if you pass true or 1 as second parameter, then only one document will be deleted.
Syntax
Following is the syntax of the remove() method −
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
Example
Assume we have created a collection and inserted 5 documents into it as shown below −
> use testDB switched to db testDB > db.createCollection("myColl") { "ok" : 1 } > data = [ ... {"_id": "1001", "name": "Ram", "age": "26", "city": "Hyderabad"}, ... {"_id": "1002", "name": "Rahim", "age": 27, "city": "Bangalore"}, ... {"_id": "1003", "name": "Robert", "age": 28, "city": "Mumbai"}, ... {"_id": "1004", "name": "Romeo", "age": 25, "city": "Pune"}, ... {"_id": "1005", "name": "Sarmista", "age": 23, "city": "Delhi"}, ... {"_id": "1006", "name": "Rasajna", "age": 26, "city": "Chennai"} ] > db.sample.insert(data) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 6, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
Following query deletes the document(s) of the collection which have name value as Sarmista.
> db.sample.remove({"name": "Sarmista"}) WriteResult({ "nRemoved" : 1 }) > db.sample.find() { "_id" : "1001", "name" : "Ram", "age" : "26", "city" : "Hyderabad" } { "_id" : "1002", "name" : "Rahim", "age" : 27, "city" : "Bangalore" } { "_id" : "1003", "name" : "Robert", "age" : 28, "city" : "Mumbai" } { "_id" : "1004", "name" : "Romeo", "age" : 25, "city" : "Pune" } { "_id" : "1006", "name" : "Rasajna", "age" : 26, "city" : "Chennai" }
If you invoke remove() method without passing deletion criteria, all the documents in the collection will be deleted.
> db.sample.remove({}) WriteResult({ "nRemoved" : 5 }) > db.sample.find()
Deleting Documents Using Python
To delete documents from a collection of MangoDB, you can delete documents from a collections using the methods delete_one() and delete_many() methods.
These methods accept a query object specifying the condition for deleting documents.
The detele_one() method deletes a single document, in case of a match. If no query is specified this method deletes the first document in the collection.
Example
Following python example deletes the document in the collection which has id value as 1006.
from pymongo import MongoCpent #Creating a pymongo cpent cpent = MongoCpent( localhost , 27017) #Getting the database instance db = cpent[ lpaksgf ] #Creating a collection coll = db[ example ] #Inserting document into a collection data = [ {"_id": "1001", "name": "Ram", "age": "26", "city": "Hyderabad"}, {"_id": "1002", "name": "Rahim", "age": "27", "city": "Bangalore"}, {"_id": "1003", "name": "Robert", "age": "28", "city": "Mumbai"}, {"_id": "1004", "name": "Romeo", "age": 25, "city": "Pune"}, {"_id": "1005", "name": "Sarmista", "age": 23, "city": "Delhi"}, {"_id": "1006", "name": "Rasajna", "age": 26, "city": "Chennai"} ] res = coll.insert_many(data) print("Data inserted ......") #Deleting one document coll.delete_one({"_id" : "1006"}) #Retrieving all the records using the find() method print("Documents in the collection after update operation: ") for doc2 in coll.find(): print(doc2)
Output
Data inserted ...... Documents in the collection after update operation: { _id : 1001 , name : Ram , age : 26 , city : Hyderabad } { _id : 1002 , name : Rahim , age : 27 , city : Bangalore } { _id : 1003 , name : Robert , age : 28 , city : Mumbai } { _id : 1004 , name : Romeo , age : 25, city : Pune } { _id : 1005 , name : Sarmista , age : 23, city : Delhi }
Similarly, the delete_many() method of pymongo deletes all the documents that satisfies the specified condition.
Example
Following example deletes all the documents in the collection whose age value is greater than 26 −
from pymongo import MongoCpent #Creating a pymongo cpent cpent = MongoCpent( localhost , 27017) #Getting the database instance db = cpent[ sampleDB ] #Creating a collection coll = db[ example ] #Inserting document into a collection data = [ {"_id": "1001", "name": "Ram", "age": "26", "city": "Hyderabad"}, {"_id": "1002", "name": "Rahim", "age": "27", "city": "Bangalore"}, {"_id": "1003", "name": "Robert", "age": "28", "city": "Mumbai"}, {"_id": "1004", "name": "Romeo", "age": "25", "city": "Pune"}, {"_id": "1005", "name": "Sarmista", "age": "23", "city": "Delhi"}, {"_id": "1006", "name": "Rasajna", "age": "26", "city": "Chennai"} ] res = coll.insert_many(data) print("Data inserted ......") #Deleting multiple documents coll.delete_many({"age":{"$gt":"26"}}) #Retrieving all the records using the find() method print("Documents in the collection after update operation: ") for doc2 in coll.find(): print(doc2)
Output
Data inserted ...... Documents in the collection after update operation: { _id : 1001 , name : Ram , age : 26 , city : Hyderabad } { _id : 1004 , name : Romeo , age : 25 , city : Pune } { _id : 1005 , name : Sarmista , age : 23 , city : Delhi } { _id : 1006 , name : Rasajna , age : 26 , city : Chennai }
If you invoke the delete_many() method without passing any query, this method deletes all the documents in the collection.
coll.delete_many({})Advertisements