pymongo 删除数据相关

 

转载于https://www.cnblogs.com/mingerlcm/p/10699553.html

 

使用user集合,删除user集合的数据

pymongo 删除数据相关
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)


pymongo 删除数据相关

 

pymongo 删除数据相关
{'_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': '男'}
pymongo 删除数据相关

 

 

 

delete_one(): 

删除一条数据

 

 

pymongo 删除数据相关
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
pymongo 删除数据相关

 

deleted_count 返回删除的条目数据

delete_many() :
删除多条数据
pymongo 删除数据相关
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
上一篇:python MongoDB


下一篇:API爬取天气预报数据