【发布时间】:2021-04-10 04:27:28
【问题描述】:
我在 Flask 中构建了一个小脚本,它从我的 MongoDB 中获取一个带有 vkey 的文件,并更新文档中的学分和级别:
client = pymongo.MongoClient("mongodb+srv://root:<password>@cluster0.3ypph.mongodb.net/data?retryWrites=true&w=majority")
db=client["data"]
col=db["user_currency"]
h=hmac.new(b"test",b"",hashlib.sha3_512)
credits_update=credits-cost
h.update(vkey.encode("utf8"))
try:
db.col.update_one(
{"vkey":h.hexdigest()},
{"$set":{"credits":str(credits_update)}}
)
db.col.update_one(
{"vkey":h.hexdigest()},
{
"$set":{"level":count},
"$currentDate": { "lastModified": True }
}
)
except:
return redirect("/currency?error=02")
else:
return redirect("/currency?bought=lvlboost")
但是,执行后MongoDB中没有任何更新,它只返回到目标页面/currency?bought=lvlboost。我重新加载了数据库并检查了 vkey 的正确性。两者是相同的。有谁知道可能是什么问题?
【问题讨论】:
-
首先获取每个
update_one的返回值;检查matched_count和modified_count看看是否有任何更新发生。 -
这些计数的结果是什么?
标签: python mongodb flask pymongo