From: delacruzramo Date: Tue, 11 Feb 2020 11:14:07 +0000 (+0000) Subject: Add unset/push/pull features to set_list X-Git-Tag: v7.1.0rc1~9 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=f71fcff0e66c178f6edcaf7c6f35b15eafe9f3ba;p=osm%2Fcommon.git Add unset/push/pull features to set_list Change-Id: I2889a1fcafa3011141b4fb4480a586671d4e13ce Signed-off-by: delacruzramo --- diff --git a/osm_common/dbbase.py b/osm_common/dbbase.py index 7f87663..614fa29 100644 --- a/osm_common/dbbase.py +++ b/osm_common/dbbase.py @@ -159,12 +159,18 @@ class DbBase(object): """ raise DbException("Method 'set_one' not implemented") - def set_list(self, table, q_filter, update_dict): + def set_list(self, table, q_filter, update_dict, unset=None, pull=None, push=None): """ Modifies al matching entries at database :param table: collection or table :param q_filter: Filter :param update_dict: Plain dictionary with the content to be updated. It is a dot separated keys and a value + :param unset: Plain dictionary with the content to be removed if exist. It is a dot separated keys, value is + 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 :return: Dict with the number of entries modified """ raise DbException("Method 'set_list' not implemented") diff --git a/osm_common/dbmongo.py b/osm_common/dbmongo.py index 6eb5ef5..16da6e7 100644 --- a/osm_common/dbmongo.py +++ b/osm_common/dbmongo.py @@ -382,18 +382,33 @@ class DbMongo(DbBase): except Exception as e: # TODO refine raise DbException(e) - def set_list(self, table, q_filter, update_dict): + def set_list(self, table, q_filter, update_dict, unset=None, pull=None, push=None): """ Modifies al matching entries at database :param table: collection or table :param q_filter: Filter :param update_dict: Plain dictionary with the content to be updated. It is a dot separated keys and a value + :param unset: Plain dictionary with the content to be removed if exist. It is a dot separated keys, value is + 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 :return: Dict with the number of entries modified """ try: + db_oper = {} + if update_dict: + db_oper["$set"] = update_dict + if unset: + db_oper["$unset"] = unset + if pull: + db_oper["$pull"] = pull + if push: + db_oper["$push"] = push with self.lock: collection = self.db[table] - rows = collection.update_many(self._format_filter(q_filter), {"$set": update_dict}) + rows = collection.update_many(self._format_filter(q_filter), db_oper) return {"modified": rows.modified_count} except Exception as e: # TODO refine raise DbException(e)