1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| [root@master mongodb]# cat >demo8.py<<EOF #!/usr/bin/python3
import pymongo myclient = pymongo.MongoClient("mongodb://admin:123456@localhost:27017/") mydb = myclient["runoobdb"] mycol = mydb["sites"] myquery = { "alexa": "10000" } newvalues = { "$set": { "alexa": "12345" } } mycol.update_one(myquery, newvalues) # 输出修改后的 "sites" 集合 for x in mycol.find(): print(x) EOF
# 测试 [root@master mongodb]# python3 demo8.py {'_id': ObjectId('627011d8468e421bd3932e64'), 'name': 'RUNOOB', 'alexa': '12345', 'url': 'https://www.runoob.com'} {'_id': ObjectId('62701243450eec8de54febe0'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com'} {'_id': ObjectId('62701243450eec8de54febe1'), 'name': 'QQ', 'alexa': '101', 'url': 'https://www.qq.com'} {'_id': ObjectId('62701243450eec8de54febe2'), 'name': 'Facebook', 'alexa': '10', 'url': 'https://www.facebook.com'} {'_id': ObjectId('62701243450eec8de54febe3'), 'name': '知乎', 'alexa': '103', 'url': 'https://www.zhihu.com'} {'_id': ObjectId('62701243450eec8de54febe4'), 'name': 'Github', 'alexa': '109', 'url': 'https://www.github.com'} {'_id': ObjectId('627012e623e1c247c715ced7'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com'} {'_id': ObjectId('627012e623e1c247c715ced8'), 'name': 'QQ', 'alexa': '101', 'url': 'https://www.qq.com'} {'_id': ObjectId('627012e623e1c247c715ced9'), 'name': 'Facebook', 'alexa': '10', 'url': 'https://www.facebook.com'} {'_id': ObjectId('627012e623e1c247c715ceda'), 'name': '知乎', 'alexa': '103', 'url': 'https://www.zhihu.com'} {'_id': ObjectId('627012e623e1c247c715cedb'), 'name': 'Github', 'alexa': '109', 'url': 'https://www.github.com'}
|