I need to query MongoDB and check if an item exists or not:
class MongoDBPipeline(object):
def __init__(self):
connection = pymongo.MongoClient(
settings['MONGODB_SERVER'],
settings['MONGODB_PORT']
)
db = connection[settings['MONGODB_DB']]
self.collection = db[settings['MONGODB_COLLECTION']]
def process_item(self, item, spider):
dup_check = self.collection.find({'image':item['image']}).count()
if dup_check == 0 :
self.collection.insert(dict(item))
log.msg("Image added to MongoDB database!",
level=log.DEBUG, spider=spider)
print "image Added!"
else:
print "Image Exist"
return item
For now it works, but I want to know if I am doing it the right way or if there is a better solution.