Python Mongodb如何使用Delete_many()?

2021年3月19日18:34:54 发表评论 824 次浏览

MongoDB是为现代应用程序开发人员和云构建的基于文档的通用分布式数据库。这是一个文档数据库, 这意味着它将数据存储在类似JSON的文档中。这是一种考虑数据的有效方法, 比传统的表格模型更具表现力和功能。

Delete_many()

Delete_many()当需要删除多个文档时使用。创建一个包含要删除的文档的查询对象, 并将其作为第一个参数传递给delete_many()。

样本数据库:

python-mongodb-delete-many-1

范例1:删除名称以" A"开头的所有文档。

import pymongo
  
  
client = pymongo.MongoClient( "mongodb://localhost:27017/" )
  
# Connecting to the database
mydb = client[ "GFG" ]
  
# Connecting the to collection
col = mydb[ "Geeks" ]
  
query = { "Name" : { "$regex" : "^A" }}
d = col.delete_many(query)
  
print (d.deleted_count, " documents deleted !!" )

输出如下:

2  documents deleted !!

MongoDB Shell:

python-mongodb-delet-many-2

范例2:

import pymongo
  
  
client = pymongo.MongoClient( "mongodb://localhost:27017/" )
  
# Connecting to the database
mydb = client[ "GFG" ]
  
# Connecting the to collection
col = mydb[ "Geeks" ]
  
query = { "Class" : '3' }
d = col.delete_many(query)
  
print (d.deleted_count, " documents deleted !!" )

输出如下:

1  documents deleted !!

MongoDB Shell:

python-mongodb-delete-many-3

注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: