简体   繁体   English

python:更新MongoDB文档

[英]python: Update MongoDB document

I want to update a MongoDB document without searching for it.我想在不搜索的情况下更新 MongoDB 文档。 All the methods I've found, like updateOne() for example, first search for the document and then update it.我找到的所有方法,例如updateOne() ,首先搜索文档,然后更新它。 But I already have the document.但是我已经有了这个文件。

for newDocument in scrape:
    query = { "title": newDocument["title"] }
    oldDocument = col.find_one(query)
    if oldDocument:
        # Check if any changes
        # If has changes -> update document IF APPROVED BY USER
        print("has document")
    else:
        col.insert_one(newDocument)
        print("added")

I scan something to get a list of new dictionary objects.我扫描一些东西以获取新字典对象的列表。 I then loop through these objects to check if they exists in my database.然后我遍历这些对象以检查它们是否存在于我的数据库中。 If not, I add it.如果没有,我添加它。 If it does, I check for any changes and ask the user if they should be approved (not shown).如果是,我会检查是否有任何更改并询问用户是否应该批准(未显示)。

Is there a method to update the document without also querying for it?有没有一种方法可以在不查询的情况下更新文档? Because I already did the query and have the reference to it.因为我已经进行了查询并参考了它。

不,除了进行更新查询之外别无选择

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

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