def push(self, collection, mongo_query, push_statement): try: return self._db[collection].update( mongo_query, {"$push": push_statement}) except Exception as error: utils.print_exception()
def pull(self, collection, mongo_query, pull_statement, multi: bool = False): try: return self._db[collection].update( mongo_query, {"$pull": pull_statement}, multi = multi) except Exception as error: utils.print_exception()
def search(self, collection: str, mongo_query, fields={}, sort={}, limit=100000000): try: if fields: result = self._db[collection].find(mongo_query, fields, sort=sort, limit=limit) else: result = self._db[collection].find(mongo_query, sort=sort, limit=limit) return result except Exception as error: utils.print_exception()
def remove( self, collection: str, mongo_query: dict, multi: bool = False): try: return self._db[collection].remove( mongo_query, multi=multi) except Exception as error: utils.print_exception()
def update( self, collection: str, mongo_query: dict, set_query: dict, multi: bool = False): try: return self._db[collection].update( mongo_query, {"$set": set_query}, multi=multi) except Exception as error: utils.print_exception()
def update_chunk(chunk, collection_name): with ThreadPoolExecutor(max_workers=10) as executor, HashCodeDB() as database: try: tasks = [executor.submit( update_partner_points, stored_slide, database, collection_name) for stored_slide in chunk] updated_slides = list() aux = 0 for task in tasks: if aux%1 == 0: pass # print("{} done of {}".format(aux, len(tasks))) updated_slides.append(task.result()) aux+=1 except Exception as e: utils.print_exception() return updated_slides
def update_partner_points(stored_slide, db, collection_name): try: query = {"tags": {"$in": stored_slide["tags"]}} # print("Buscando con mismos tags") matches = db.search( collection=collection_name, mongo_query=query, fields={"_id": 1, "tags": 1, "number_of_tags": 1}) matches = [match for match in matches if (match["_id"] != stored_slide["_id"])] # print("Obteniendo puntos") partners = get_partners(stored_slide, matches) stored_slide["partners"] = partners points = [partner["points"] for partner in partners] points.extend([0, 0]) points = sorted(points)[::-1] stored_slide["best_points"] = points[0] stored_slide["best_points_2"] = points[1] # print(points) return stored_slide except Exception as error: utils.print_exception() raise error
def drop_collection(self, collection): try: return self._db[collection].drop() except Exception as error: utils.print_exception()
def insert_many(self, collection, new_elements: list): try: self._db[collection].insert_many( new_elements, bypass_document_validation=True) except BulkWriteError as bwe: utils.print_exception()
def insert_one(self, collection: str, new_element: dict): try: return self._db[collection].insert_one(new_element) except Exception as error: utils.print_exception()
def count(self, collection:str): try: return self._db[collection].count() except Exception as error: utils.print_exception()
def find_one_and_delete(self, collection:str, mongo_query: dict, sort: list = []): try: return self._db[collection].find_one_and_delete(mongo_query) except Exception as error: utils.print_exception()
def create_index(self, collection, field): try: return self._db[collection].create_index( field) except Exception as error: utils.print_exception()