[英]How do I update a Pymongo document with another dictionary?
我知道update
和update_one
方法,但是我沒有看到與使用其他字典更新文檔相關的任何內容。
例如,如果我有文件:
{
"bang": "Foo",
"bar": "baz"
}
如何使用將Foo
更改為bong
的字典{'bang': 'bong'}
更新它?
如果我沒記錯的話,你想用字典的值更新一個字段。
然后,嘗試示例代碼:
蒙戈系列:
{
"bang": "Foo",
"bar": "baz"
}
Python 程序:
from pymongo import MongoClient
mongo = MongoClient()
db = mongo.test #replace test with your db name
coll = db.testColl #replace testColl with your collection name
d = {'bang': 'bong'}
coll.update_one({}, {'$set': {'bang': d['bang']}})
Output:
{
"bang" : "bong",
"bar" : "baz"
}
如果我這不是您需要的,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.