【问题标题】:Pymongo update not updating, and with no errorsPymongo 更新不更新,并且没有错误
【发布时间】:2021-03-09 21:49:12
【问题描述】:

我有一些简单的代码可以将错误标记为已解决

doc = self.data.find_one({"id": int(errid)})
print(doc)
updt = self.data.update_one(filter={"id": errid}, update={"$set": {"fixed": True}})
doc = self.data.find_one({"id": int(errid)})
print(f"{doc}\n\n{str(updt.upserted_id)}\n\n{updt.acknowledged}")
print(f"Successfully fixed error {errid}")

(是的,所有变量都已定义)

我第一次打印doc,我得到了

{'_id': ObjectId('5fc048878dc1679d5d880f6b'), 'id': 12, 'command': 'errortest', 'fulltb': 'https://hastebin.com/otepiqihim', 'datetime': datetime.datetime(2020, 11, 27, 0, 29, 59, 708000), 'fixed': False}

第二次,我得到完全相同的结果。更新已确认 (updt.acknowleged) 但 ID 为 None (updt.upserted_id)

我也没有收到任何错误。有谁知道为什么会发生这种情况?

【问题讨论】:

    标签: python python-3.x pymongo pymongo-3.x


    【解决方案1】:

    如果您的 errid 是一个字符串,那么您将在所有地方将其转换为 int 除了 om update 命令

    updt = self.data.update_one(filter={"id": errid}, update={"$set": {"fixed": True}})
    

    应该是:

    updt = self.data.update_one(filter={"id": int(errid)}, update={"$set": {"fixed": True}})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 2013-06-26
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多