您现在的位置是:课程教程文章

python中删除文档的方法

2023-12-13 22:59课程教程文章 人已围观

1、delete_one()方法删除文档。delete_one()需要一个查询对象参数。它只删除了第一次出现。

2、在删除大量文档时,使用delete_many方法,需要查询对象。

如果我们向delete_many({})传e_many({}),它将删除集合中的所有文档。

实例

#让我们
从Flask导入FlaskimportFlask,render_template
importos#导入操作系统模块
importpymongo

MONGODB_URI='mongodb+srv://asabeneh:your_password_goes_here@30daysofpython-twxkr.mongodb.net/test?retryWrites=true&w=majority'
client=pymongo.MongoClient(MONGODB_URI)
db=client['thirty_days_of_python']#accessingthedatabase

query={'name':'John'}
db.students.delete_one(query)

forstudentindb.students.find():
print(student)
#letschecktheresultiftheageismodified
forstudentindb.students.find():
print(student)


app=Flask(__name__)
if__name__=='__main__':
#fordeploymentweusetheenviron
#tomakeitworkforbothproductionanddevelopment
port=int(os.environ.get("PORT",5000))
app.run(debug=True,host='0.0.0.0',port=port)

以上就是python中删除文档的方法,希望对大家有所帮助。更多Python学习指路:python基础教程

(推荐操作系统:windows7系统、Python 3.9.1,DELL G3电脑。)

课程教程:python中删除文档的方法

上一篇:js中ES Module和CommonJs的区别

下一篇:没有了

站点信息

  • 文章统计篇文章