# Martin Cruz # martincruz.me - Copyright 2016, All Rights Reserved import pymongo import datetime import sys connection = pymongo.MongoClient("mongodb://localhost") db = connection.final7 def remove_image(image_id): images = db.images try: result = images.delete_one({'_id': image_id}) print "num removed: ", result.deleted_count except Exception as e: print "Exception: ", type(e), e def exists_in_album(image_id): albums = db.albums try: count = albums.find({'images': {'$all': [image_id]}}).count() if count > 0: return True return False except Exception as e: print "Exception: ", type(e), e def find_images(): images = db.images try: docs = images.find() count = 0 for doc in docs: if not exists_in_album(doc['_id']): count += 1 remove_image(doc['_id']) except Exception as e: print "Exception: ", type(e), e print "Images removed: ", count find_images()