Last active
December 14, 2015 22:39
-
-
Save greggy/5159956 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf8 | |
import pymongo | |
connection = pymongo.MongoClient('localhost', 27017) | |
db = connection.photos | |
albums = db.albums | |
images = db.images | |
def remove_orphan(): | |
cursor = albums.find() | |
cursor2 = images.find() | |
images_list = [] | |
for item in cursor: | |
images_ids = item["images"] | |
for id in images_ids: | |
if not id in images_list: | |
images_list.append(id) | |
c = 0 | |
for image in cursor2: | |
id = image["_id"] | |
if not id in images_list: | |
#print id | |
c += 1 | |
images.remove({"_id": id}) | |
print c | |
remove_orphan() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment