简体   繁体   English

使用 PyMongo 更新字典

[英]Update a dict with PyMongo

I have this kind of document:我有这样的文件:

Doc1{} : 
field1, field2, field3{} :
                field1, field2, field3, field4{}:
                                        4 fields to update.

I try with this line but I got 2 issues, the first one is when is updating it's erase all of my fields in field3 and the second is that this line is in the for loop and it's erasing my field but I would like to add.我尝试使用这条线,但我遇到了 2 个问题,第一个是更新时它会擦除我在 field3 中的所有字段,第二个是这条线在 for 循环中并且它正在擦除我的字段,但我想添加。

db.doc1.update_one({field1: field1}, {"$set": {"field3": {"field4": content}}})

Ps: sorry for my approximate English. Ps:对不起我的大概英语。

To just update field4 in field3 , use the dot notation :要仅更新field3中的field4 ,请使用点表示法

record_filter = {'field1' : 'a'}
content = "d"
db.doc1.update_one(record_filter, {"$set" : {"field3.field4" : content}})

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

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