MongoDB: 更新函数在 Python Flask 中没有 return 响应
MongoDB: Update function does not return a response in 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 中没有任何更新,它只 returns 到目标页面 /currency?bought=lvlboost
。我重新加载了数据库并检查了 vkey 的正确性。两者是相同的。有谁知道可能是什么问题?
向您的查询添加一个变量,以便将 return 值添加到该变量中,
result = db.col.update_one(
{"vkey":h.hexdigest()},
{
"$set":{ "level":count, "credits":str(credits_update) },
"$currentDate": {"lastModified": True }
}
)
现在如果你 print(result)
你可以看到 matched_count
和 modified_count
。
文档:https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/#returns
我在 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 中没有任何更新,它只 returns 到目标页面 /currency?bought=lvlboost
。我重新加载了数据库并检查了 vkey 的正确性。两者是相同的。有谁知道可能是什么问题?
向您的查询添加一个变量,以便将 return 值添加到该变量中,
result = db.col.update_one(
{"vkey":h.hexdigest()},
{
"$set":{ "level":count, "credits":str(credits_update) },
"$currentDate": {"lastModified": True }
}
)
现在如果你 print(result)
你可以看到 matched_count
和 modified_count
。
文档:https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/#returns