mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-06 07:50:32 +00:00
fixed PyMongoStorage update_data method implementation
This commit is contained in:
parent
44273e17ea
commit
86170e1cb9
1 changed files with 17 additions and 8 deletions
|
|
@ -123,14 +123,23 @@ class PyMongoStorage(BaseStorage):
|
|||
|
||||
async def update_data(self, key: StorageKey, data: Mapping[str, Any]) -> Dict[str, Any]:
|
||||
document_id = self._key_builder.build(key)
|
||||
update_with = {f"data.{key}": value for key, value in data.items()}
|
||||
update_result = await self._collection.find_one_and_update(
|
||||
filter={"_id": document_id},
|
||||
update={"$set": update_with},
|
||||
upsert=True,
|
||||
return_document=True,
|
||||
projection={"_id": 0},
|
||||
)
|
||||
update_result = {}
|
||||
if not data:
|
||||
update_result = await self._collection.find_one_and_update(
|
||||
filter={"_id": document_id},
|
||||
update={"$unset": {"data": 1}},
|
||||
upsert=True,
|
||||
return_document=True,
|
||||
projection={"_id": 0},
|
||||
)
|
||||
else:
|
||||
update_result = await self._collection.find_one_and_update(
|
||||
filter={"_id": document_id},
|
||||
update={"$set": {"data": data}},
|
||||
upsert=True,
|
||||
return_document=True,
|
||||
projection={"_id": 0},
|
||||
)
|
||||
if not update_result:
|
||||
await self._collection.delete_one({"_id": document_id})
|
||||
return update_result.get("data", {})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue