python培训之删除文档的方法

发布时间:2022-12-03 09:52:59 人气:141 作者:多测师

  python中删除文档的方法

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

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

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

  实例

  # 让我们

  从 Flask 导入Flask import Flask , render_template

  import os # 导入操作系统模块

python培训之删除文档的方法

  import pymongo

  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'] # accessing the database

  query = {'name':'John'}

  db.students.delete_one(query)

  for student in db.students.find():

  print(student)

  # lets check the result if the age is modified

  for student in db.students.find():

  print(student)

  app = Flask(__name__)

  if __name__ == '__main__':

  # for deployment we use the environ

  # to make it work for both production and development

  port = int(os.environ.get("PORT", 5000))

  app.run(debug=True, host='0.0.0.0', port=port)

  以上内容为大家介绍了python培训之删除文档的方法,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注多测师。https://www.e70w.com/xwzx/


返回列表
在线客服
联系方式

热线电话

17727591462

上班时间

周一到周五

二维码
线