Python MongoDB - 删除文档

您可以使用 MongoDB 的 remove() 方法删除集合中的文档。此方法接受两个可选参数 −

  • 删除条件,指定删除文档的条件。

  • 只有一个,如果您传递 true 或 1 作为第二个参数,则只会删除一个文档。

语法

以下是 remove() 方法的语法 −

>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)

示例

假设我们创建了一个集合并向其中插入了 5 个文档,如下所示 −

> 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" : [ ]
})

以下查询删除集合中名称值为 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() 方法,则集合中的所有文档都将被删除。

> db.sample.remove({})
WriteResult({ "nRemoved" : 5 })
> db.sample.find()

使用 Python 删除文档

要从 MangoDB 集合中删除文档,可以使用 delete_one()delete_many() 方法从集合中删除文档。

这些方法接受指定删除文档条件的查询对象。

如果匹配,detele_one() 方法将删除单个文档。如果未指定查询,则此方法将删除集合中的第一个文档。

示例

以下 Python 示例将删除集合中 ID 值为 1006 的文档。

from pymongo import MongoClient

#创建 pymongo 客户端
client = MongoClient('localhost', 27017)

#获取数据库实例
db = client['lpaksgf']

#创建集合
coll = db['example']

#将文档插入集合
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 ......")

删除一个文档
coll.delete_one({"_id" : "1006"})

#使用 find() 方法检索所有记录
print("Documents in the collection after update operation: ")
for doc2 in coll.find():
   print(doc2)

输出

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() 方法删除所有满足指定条件的文档。

示例

以下示例删除集合中 age 值大于 26 − 的所有文档

from pymongo import MongoClient

#创建 pymongo 客户端
client = MongoClient('localhost', 27017)

#获取数据库实例
db = client['sampleDB']

#创建集合
coll = db['example']

#将文档插入集合
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 ......")

删除多个文档
coll.delete_many({"age":{"$gt":"26"}})

#使用 find() 方法检索所有记录
print("Documents in the collection after update operation: ")
for doc2 in coll.find():
   print(doc2)

输出

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() 方法而不传递任何查询,则此方法将删除集合中的所有文档。

coll.delete_many({})