• 技术文章 >Python技术 >Python基础教程

    python中删除文档的方法

    小妮浅浅小妮浅浅2021-11-09 20:45:22原创19370

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

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

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

    实例

    # 让我们
    从 Flask 导入Flask import Flask , render_template
    import  os  # 导入操作系统模块
    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学习指路:python基础教程

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

    专题推荐:python 删除
    品易云
    上一篇:python中if-elif-else语句的使用注意 下一篇:python函数定义的规则

    相关文章推荐

    • python os.popen方法是什么• python中subprocess的用法• 如何走进Python的大门?• python蒙特卡洛算法的介绍• python如何过滤列表中的唯一值• python列表推导式的结构探究• python中condition条件变量的作用• python单元测试中的函数整理• TIOBE 10月编程语言排行榜,Python位临榜首• python中lambdas匿名函数的用法• python运算符的优先级规则• python中断言的使用注意• python正则表达式如何匹配内容• python正则表达式查找和替换内容

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网