def update_one_origin(self, db_name, collection_name, col_filter, col_update, options=None, create_on_absence=False, is_many=False, is_extra=False, **kwargs): col = self.get_origin_collection(db_name, collection_name, create_on_absence=create_on_absence) if not col: raise CollectionNotFoundException(msg='Cannot find collection with name ' + collection_name) if is_extra: now_timestamp = datetime.now().timestamp() if '$setOnInsert' in col_update: col_update["$setOnInsert"]['created'] = now_timestamp else: col_update["$setOnInsert"] = {"created": now_timestamp} if "$set" in col_update: col_update["$set"]["modified"] = now_timestamp if not kwargs.get('modified') else kwargs.get('modified') if is_many: result = col.update_many(convert_oid(col_filter), convert_oid(col_update, update=True), **(options if options else {})) else: result = col.update_one(convert_oid(col_filter), convert_oid(col_update, update=True), **(options if options else {})) return { "acknowledged": result.acknowledged, "matched_count": result.matched_count, "modified_count": result.modified_count, "upserted_id": str(result.upserted_id) if result.upserted_id else '', }
def update_document(self, collection_name, json_body, is_update_one): user_did, app_did, col = self.__get_collection(collection_name, VAULT_ACCESS_WR) update = json_body["update"] if "$set" in update: update["$set"]["modified"] = datetime.utcnow() if is_update_one: ret = col.update_one( convert_oid(json_body["filter"]), convert_oid(update, update=True), **options_filter(json_body, ("upsert", "bypass_document_validation"))) else: ret = col.update_many( convert_oid(json_body["filter"]), convert_oid(update, update=True), **options_filter(json_body, ("upsert", "bypass_document_validation"))) update_used_storage_for_mongodb_data( user_did, get_mongo_database_size(user_did, app_did)) return { "acknowledged": ret.acknowledged, "matched_count": ret.matched_count, "modified_count": ret.modified_count, "upserted_id": str(ret.upserted_id) if ret.upserted_id else None }
def delete_document(self, collection_name, col_filter, is_delete_one): user_did, app_did, col = self.__get_collection(collection_name, VAULT_ACCESS_WR) if is_delete_one: col.delete_one(convert_oid(col_filter)) else: col.delete_many(convert_oid(col_filter)) update_used_storage_for_mongodb_data( user_did, get_mongo_database_size(user_did, app_did))
def find_many(self, user_did, app_did, collection_name, col_filter, options=None, throw_exception=True): col = self.get_user_collection(user_did, app_did, collection_name) if not col: if not throw_exception: return [] raise CollectionNotFoundException(msg='Cannot find collection with name ' + collection_name) return list(col.find(convert_oid(col_filter) if col_filter else None, **(options if options else {})))
def count_document(self, collection_name, json_body): user_did, app_did, col = self.__get_collection(collection_name, VAULT_ACCESS_R) count = col.count_documents( convert_oid(json_body["filter"] if json_body and 'filter' in json_body else {}), **options_filter(json_body, ("skip", "limit", "maxTimeMS"))) return {"count": count}
def find_one_origin(self, db_name, collection_name, col_filter, options=None, create_on_absence=False, throw_exception=True): col = self.get_origin_collection(db_name, collection_name, create_on_absence=create_on_absence) if not create_on_absence and not col: if not throw_exception: return None raise CollectionNotFoundException(msg='Cannot find collection with name ' + collection_name) return col.find_one(convert_oid(col_filter) if col_filter else None, **(options if options else {}))
def delete_one_origin(self, db_name, collection_name, col_filter, is_check_exist=True): col = self.get_origin_collection(db_name, collection_name) if not col: if is_check_exist: raise CollectionNotFoundException(msg='Cannot find collection with name ' + collection_name) else: return {"acknowledged": False, "deleted_count": 0} result = col.delete_one(convert_oid(col_filter)) return { "acknowledged": result.acknowledged, "deleted_count": result.deleted_count, }
def insert_one_origin(self, db_name, collection_name, document, options=None, create_on_absence=False, is_extra=True, **kwargs): col = self.get_origin_collection(db_name, collection_name, create_on_absence) if not col: raise CollectionNotFoundException(msg='Cannot find collection with name ' + collection_name) if is_extra: now_timestamp = datetime.now().timestamp() document['created'] = now_timestamp if not kwargs.get('created') else kwargs.get('created') document['modified'] = now_timestamp if not kwargs.get('modified') else kwargs.get('modified') result = col.insert_one(convert_oid(document), **(options if options else {})) return { "acknowledged": result.acknowledged, "inserted_id": str(result.inserted_id) if result.inserted_id else '' }
def insert_document(self, collection_name, json_body): user_did, app_did, col = self.__get_collection(collection_name, VAULT_ACCESS_WR) documents = [] for document in json_body["document"]: document["created"] = datetime.utcnow() document["modified"] = datetime.utcnow() documents.append(convert_oid(document)) ret = col.insert_many( documents, **options_filter(json_body, ("bypass_document_validation", "ordered"))) update_used_storage_for_mongodb_data( user_did, get_mongo_database_size(user_did, app_did)) return { "acknowledged": ret.acknowledged, "inserted_ids": [str(_id) for _id in ret.inserted_ids] }
def __upsert_script_to_database(self, script_name, json_data, user_did, app_did): col = cli.get_user_collection(user_did, app_did, SCRIPTING_SCRIPT_COLLECTION, create_on_absence=True) json_data['name'] = script_name fix_dollar_keys(json_data['executable']) ret = col.replace_one({"name": script_name}, convert_oid(json_data), upsert=True, bypass_document_validation=False) return { "acknowledged": ret.acknowledged, "matched_count": ret.matched_count, "modified_count": ret.modified_count, "upserted_id": str(ret.upserted_id) if ret.upserted_id else '', }
def __is_satisfied_query_has_result(self, con_body_data, context): col_name = con_body_data['collection'] col_filter = con_body_data.get('filter', {}) msg = populate_with_params_values(self.user_did, self.app_did, col_filter, self.params) if msg: raise BadRequestException(msg='Cannot find parameter: ' + msg) col = cli.get_user_collection(context.target_did, context.target_app_did, col_name) if not col: raise BadRequestException( msg='Do not find condition collection with name ' + col_name) # INFO: 'options' is the internal supporting. options = populate_options_count_documents( con_body_data.get('options', {})) return col.count_documents(convert_oid(col_filter), **options) > 0
def __do_find(self, col, col_filter, options): ret = col.find(convert_oid(col_filter if col_filter else {}), **options) return {"items": [c for c in json.loads(json_util.dumps(ret))]}