Skip to content

Instantly share code, notes, and snippets.

@greggy
Last active December 14, 2015 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greggy/5159956 to your computer and use it in GitHub Desktop.
Save greggy/5159956 to your computer and use it in GitHub Desktop.
# 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