提交成功
提交失败
Python操作MongoDB入门五:修改数据

1、单条更新

基本语法

collection.update_one(查询条件字典, {'$set': 被更新的数据字典})

实现代码

__author__ = "dengxinyan"

from pymongo import MongoClient

conn = MongoClient(host='localhost', port=27017, username=None, password=None)

database = conn['spider']
collection = database['news']

# 只更新满足条件的第一条数据
res = collection.update_one({'title': "2022年10月1日早间新闻"}, {'$set': {'title': "2022年10月1日早间新闻2"}})
print(res)

2、多条更新

基本语法

collection.update_many(查询条件字典, {'$set': 被更新的数据字典})

实现代码

__author__ = "dengxinyan"

from pymongo import MongoClient

conn = MongoClient(host='localhost', port=27017, username=None, password=None)

database = conn['spider']
collection = database['news']

# 更新满足条件的所有数据
res = collection.update_many({'title': "2022年10月1日早间新闻2"}, {'$set': {'title': "2022年10月1日早间新闻3"}})
print(res)


免责声明:本站的所有博客、言论等仅代表作者个人观点,与本网站立场无关。本站公开发布此信息的目的在于传播更多信息,如有侵权请立即与我们联系,我们将及时处理。

点赞 0
评论 0
分享
收藏
打赏
举报