Skip to content

Commit

Permalink
Make the insertions to mongodb atlas as bulk insert
Browse files Browse the repository at this point in the history
  • Loading branch information
SiddharthaAnand committed Dec 1, 2018
1 parent 1c54f3c commit f5c5042
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions coauthornetwork/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(self, mongo_uri, mongo_db):
self.mongo_db = mongo_db
self.db = None
self.client = None
self.items = []

@classmethod
def from_crawler(cls, crawler):
Expand All @@ -33,10 +34,18 @@ def open_spider(self, spider):

def process_item(self, item, spider):
try:
self.db[self.collection_name].insert_one(dict(item))
self.items.append(item)
if len(self.items) % 10 == 0:
print "length: ", len(self.items)
self.db[self.collection_name].insert_many(self.items)
print "-----------------------BULK INSERT COMPLETE--------------------"
self.items = []
else:
print "False condition"
return item
except Exception:
raise DropItem()

def close_spider(self, item, spider):
self.client.close()
self.client.close()

0 comments on commit f5c5042

Please sign in to comment.