MongoDB:Update函数在Python Flask中不返回响应



我在Flask中构建了一个小脚本,它使用vkey从我的MongoDB中获取一个文件,并更新文档中的信用和级别:

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的正确性。两者完全相同。有人知道问题出在哪里吗?

向查询中添加一个变量,以便将返回值添加到该变量中,

result = db.col.update_one(
{"vkey":h.hexdigest()},
{
"$set":{ "level":count, "credits":str(credits_update) },
"$currentDate": {"lastModified": True } 
}
)

现在,如果您使用print(result),您可以看到matched_countmodified_count

文件:https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/#returns

最新更新