Python Data Access 简明教程
Python MongoDB - Delete Document
您可以使用 MongoDB 的 remove() 方法来删除集合中的文档。此方法接受两个可选参数:
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
以下是 remove() 方法的语法 −
Following is the syntax of the remove() method −
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
Example
假设我们已经创建了一个集合,并在其中插入了 5 个文档,如下所示 −
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" : [ ]
})
以下查询将删除具有 name 值为 Sarmista 的集合文档。
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" }
如果你调用 remove() 方法而没有传递删除条件,则集合中的所有文档都将被删除。
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
要从 MangoDB 的集合中删除文档,你可以使用 delete_one() 和 delete_many() 方法从集合中删除文档。
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.
detete_one() 方法删除单个文档(如果匹配)。如果未指定查询,则此方法将删除集合中的第一个文档。
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
以下 python 示例删除了集合中 id 值为 1006 的文档。
Following python example deletes the document in the collection which has id value as 1006.
from pymongo import MongoClient
#Creating a pymongo client
client = MongoClient('localhost', 27017)
#Getting the database instance
db = client['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'}
同样,pymongo 的 delete_many() 方法删除满足指定条件的所有文档。
Similarly, the delete_many() method of pymongo deletes all the documents that satisfies the specified condition.
Example
以下示例删除了集合中 age 值大于 26 的所有文档 −
Following example deletes all the documents in the collection whose age value is greater than 26 −
from pymongo import MongoClient
#Creating a pymongo client
client = MongoClient('localhost', 27017)
#Getting the database instance
db = client['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'}
如果你调用 delete_many() 方法而未传递任何查询,则此方法将删除集合中的所有文档。
If you invoke the delete_many() method without passing any query, this method deletes all the documents in the collection.
coll.delete_many({})