MongoDB pymongo模块 删除数据
Posted minger_lcm
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB pymongo模块 删除数据相关的知识,希望对你有一定的参考价值。
使用user表,删除user表数据
import pymongo mongo_client = pymongo.MongoClient( host=‘192.168.0.112‘, port=27017, username="admin", password="123456" ) mongo_db = mongo_client["db1"] # 删除数据 res = mongo_db.user.find() for i in res: print(i)
{‘_id‘: ObjectId(‘5ca7a4b0219efd687462f965‘), ‘id‘: 1.0, ‘name‘: ‘jack‘, ‘age‘: 73.0} {‘_id‘: ObjectId(‘5ca7a4b7219efd687462f966‘), ‘id‘: 2.0, ‘name‘: ‘mike‘, ‘age‘: 84.0, ‘gender‘: ‘男‘} {‘_id‘: ObjectId(‘5ca7a4c4219efd687462f967‘), ‘id‘: 3.0, ‘name‘: ‘peter‘, ‘age‘: 21.0} {‘_id‘: ObjectId(‘5ca7a4c4219efd687462f968‘), ‘id‘: 4.0, ‘name‘: ‘xiaogang‘, ‘age‘: 34.0, ‘hobby‘: [‘篮球‘]} {‘_id‘: ObjectId(‘5ca7a4c4219efd687462f969‘), ‘id‘: 5.0, ‘name‘: ‘ben‘, ‘age‘: 24.0} {‘_id‘: ObjectId(‘5ca7a505219efd687462f96a‘), ‘id‘: 6.0, ‘name‘: ‘Mary‘, ‘age‘: 84.0, ‘gender‘: ‘男‘}
delete_one():
删除一条数据
import pymongo mongo_client = pymongo.MongoClient( host=‘192.168.0.112‘, port=27017, username="admin", password="123456" ) mongo_db = mongo_client["db1"] # 删除数据 res = mongo_db.user.delete_one({"age":21}) print(res,res.deleted_count) # deleted_count 返回删除的条目数据 # 返回对象 # <pymongo.results.DeleteResult object at 0x0000000002EDBF08> # 返回1 代表删除1条数据 # 1
deleted_count 返回删除的条目数据
delete_many() :
删除多条数据
import pymongo mongo_client = pymongo.MongoClient( host=‘192.168.0.112‘, port=27017, username="admin", password="123456" ) mongo_db = mongo_client["db1"] # 删除数据 res = mongo_db.user.delete_many({"age":84}) print(res,res.deleted_count) # deleted_count 返回删除的条目数据 # 返回对象 # <pymongo.results.DeleteResult object at 0x0000000002EDBF08> # 返回2 代表删除1条数据 # 2
以上是关于MongoDB pymongo模块 删除数据的主要内容,如果未能解决你的问题,请参考以下文章