Fix MongoObject.update
This commit is contained in:
parent
967d94242e
commit
2626f91c7b
1 changed files with 12 additions and 10 deletions
|
@ -4,10 +4,9 @@ from datetime import datetime
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
from bson import ObjectId
|
from bson import ObjectId
|
||||||
from pymongo import ASCENDING, DESCENDING
|
|
||||||
|
|
||||||
from jarvis.config import get_config
|
from jarvis.config import get_config
|
||||||
from jarvis.db import DBManager
|
from jarvis.db import DBManager
|
||||||
|
from pymongo import ASCENDING, DESCENDING
|
||||||
|
|
||||||
logger = logging.getLogger("mongodb")
|
logger = logging.getLogger("mongodb")
|
||||||
|
|
||||||
|
@ -128,15 +127,18 @@ class MongoObject:
|
||||||
:return: If update was successful
|
:return: If update was successful
|
||||||
"""
|
"""
|
||||||
to_update = self.to_dict()
|
to_update = self.to_dict()
|
||||||
search = {"_id": to_update["_id"]}
|
if self._id:
|
||||||
try:
|
search = {"_id": to_update["_id"]}
|
||||||
result = self.coll.update_one(
|
try:
|
||||||
search, {"$set": to_update}, upsert=True
|
result = self.coll.update_one(
|
||||||
)
|
search, {"$set": to_update}, upsert=True
|
||||||
except Exception as e:
|
)
|
||||||
logger.error(f"Failed to update {type(self).__name__}", e)
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to update {type(self).__name__}", e)
|
||||||
|
else:
|
||||||
|
return result.modified_count > 0
|
||||||
else:
|
else:
|
||||||
return result.modified_count > 0
|
self.insert()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get(cls, **kwargs) -> Optional[object]:
|
def get(cls, **kwargs) -> Optional[object]:
|
||||||
|
|
Loading…
Add table
Reference in a new issue