简体   繁体   English

如何更新文档并使用 Pymongo 插入字段

[英]How can I update a document and insert a field using Pymongo

I'm making a program that reads CNPJ (in Brazil it's called that, in the US there is something similar, the EIN)我正在制作一个读取 CNPJ 的程序(在巴西它被称为,在美国有类似的东西,EIN)

I already have the data in my MongoDB. Now that we add a new field eg Test with the value testtest我的 MongoDB 中已经有了数据。现在我们添加一个新字段,例如Test ,其值为testtest

how can I do this?我怎样才能做到这一点? Because the way I'm doing it, it's not working.因为我这样做的方式是行不通的。

 collection.update_one({'Result': '0'}, {"$set": {"Test": "testtest"}})

in the image below: I would like to insert the new data inside Lawsuits在下图中:我想在 Lawsuits 中插入新数据

在此处输入图像描述

Try the find query inside update_one using _id so that you can get the exact match and then update it, it should work.使用_idupdate_one中尝试find查询,以便您可以获得完全匹配,然后更新它,它应该可以工作。

for reference:以供参考:

collection.update_one({'_id': ObjectId(_id)},
                      {"$set": {"Test": "testtest"}})

Let me know if you have any additional requirement.如果您有任何其他要求,请告诉我。

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

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