简体   繁体   English

如何用另一个字典更新 Pymongo 文档?

[英]How do I update a Pymongo document with another dictionary?

I am aware of the update and update_one methods, however I haven't seen anything related to updating documents with other dictionaries.我知道updateupdate_one方法,但是我没有看到与使用其他字典更新文档相关的任何内容。

For example, if I have the document:例如,如果我有文件:

{
  "bang": "Foo",
  "bar": "baz"
}

How can I update it with a dictionary {'bang': 'bong'} that changes Foo to bong ?如何使用将Foo更改为bong的字典{'bang': 'bong'}更新它?

If I am not wrong, you want to update a field with a dictionary's value.如果我没记错的话,你想用字典的值更新一个字段。

Then, try the sample code:然后,尝试示例代码:

Mongo Collection:蒙戈系列:

{
  "bang": "Foo",
  "bar": "baz"
}

Python Program: 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: Output:

{
    "bang" : "bong",
    "bar" : "baz"
}

If I this isn't what you need, then let me know.如果我这不是您需要的,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM