X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=osm_common%2Fdbmongo.py;h=0784985a37c5f708fe666f93032526091887a588;hb=399f6c3acc2e8852d463844a562bcde063c912e2;hp=e40d0e4a4705dc072befa6b04123c88323e4884e;hpb=7982b78adc05fc52c2259d900004ca570e38bdbf;p=osm%2Fcommon.git diff --git a/osm_common/dbmongo.py b/osm_common/dbmongo.py index e40d0e4..0784985 100644 --- a/osm_common/dbmongo.py +++ b/osm_common/dbmongo.py @@ -362,7 +362,8 @@ class DbMongo(DbBase): except Exception as e: # TODO refine raise DbException(e) - def set_one(self, table, q_filter, update_dict, fail_on_empty=True, unset=None, pull=None, push=None): + def set_one(self, table, q_filter, update_dict, fail_on_empty=True, unset=None, pull=None, push=None, + push_list=None): """ Modifies an entry at database :param table: collection or table @@ -376,6 +377,8 @@ class DbMongo(DbBase): if exist in the array is removed. If not exist, it is ignored :param push: Plain dictionary with the content to be appended to an array. It is a dot separated keys and value is appended to the end of the array + :param push_list: Same as push but values are arrays where each item is and appended instead of appending the + whole array :return: Dict with the number of entries modified. None if no matching is found. """ try: @@ -386,8 +389,10 @@ class DbMongo(DbBase): db_oper["$unset"] = unset if pull: db_oper["$pull"] = pull - if push: - db_oper["$push"] = push + if push or push_list: + db_oper["$push"] = push or {} + if push_list: + db_oper["$push"].update({k: {"$each": v} for k, v in push_list.items()}) with self.lock: collection = self.db[table] @@ -401,7 +406,7 @@ class DbMongo(DbBase): except Exception as e: # TODO refine raise DbException(e) - def set_list(self, table, q_filter, update_dict, unset=None, pull=None, push=None): + def set_list(self, table, q_filter, update_dict, unset=None, pull=None, push=None, push_list=None): """ Modifies al matching entries at database :param table: collection or table @@ -411,8 +416,10 @@ class DbMongo(DbBase): ignored. If not exist, it is ignored :param pull: Plain dictionary with the content to be removed from an array. It is a dot separated keys and value if exist in the array is removed. If not exist, it is ignored - :param push: Plain dictionary with the content to be appended to an array. It is a dot separated keys and value - is appended to the end of the array + :param push: Plain dictionary with the content to be appended to an array. It is a dot separated keys, the + single value is appended to the end of the array + :param push_list: Same as push but values are arrays where each item is and appended instead of appending the + whole array :return: Dict with the number of entries modified """ try: @@ -423,8 +430,10 @@ class DbMongo(DbBase): db_oper["$unset"] = unset if pull: db_oper["$pull"] = pull - if push: - db_oper["$push"] = push + if push or push_list: + db_oper["$push"] = push or {} + if push_list: + db_oper["$push"].update({k: {"$each": v} for k, v in push_list.items()}) with self.lock: collection = self.db[table] rows = collection.update_many(self._format_filter(q_filter), db_oper)