def fmt(x): if x['op'] == 'i': op = x['o'] return pymongo.UpdateOne({'_id': op['_id']}, {'$set': op}, upsert=True) elif x['op'] == 'u': return pymongo.UpdateOne(x['o2'], x['o'], upsert=True) elif x['op'] == 'd': return pymongo.DeleteMany(x['o'])
def removeWithQuery(self, query): """ Remove all documents matching a given query from the collection. For safety reasons, you may not pass an empty query. Note: this does NOT return a Mongo DeleteResult. :param query: The search query for documents to delete, see general MongoDB docs for "find()" :type query: dict """ assert query self.collection.bulk_write([pymongo.DeleteMany(query)], ordered=False)
def delete_cache(db, collection, cursor, delay): ids_to_remove = [] files_to_remove = [] for l in cursor: ids_to_remove.append(l['_id']) files_to_remove.append((l['bam'], l['bai'])) if len(ids_to_remove) > 0: requests = [pymongo.DeleteMany({'_id': {'$in': ids_to_remove}})] result = db[collection].bulk_write(requests) if len(files_to_remove) > 0: time.sleep( delay ) # sleep a little to allow finish any active downloads of selected cached files for bam, bai in files_to_remove: try: os.remove(os.path.join(cache_dir, bam)) os.remove(os.path.join(cache_dir, bai)) except OSError: pass
def removeWithQuery(self, query): """ Remove all documents matching a given query from the collection. For safety reasons, you may not pass an empty query. Note: this does NOT return a Mongo DeleteResult. :param query: The search query for documents to delete, see general MongoDB docs for "find()" :type query: dict """ if not query: raise Exception('query must be specified') attachedQuery = query.copy() attachedQuery['datafile'] = {'$exists': True} for element in self.collection.find(attachedQuery): file = File().load(element['datafile']['fileId'], force=True) if file: File().remove(file) self.collection.bulk_write([pymongo.DeleteMany(query)], ordered=False)
def delete_all_db(): db.bulk_write([pymongo.DeleteMany({})])