I want to use upsert with MongoDB and pymongo to add about 10 data items, but only 1 item is added. How can I add 10 cases?
Of course it is possible to get 10 cases using insert,
but I want to use upsert because I want to add the data when it is updated.
Thanks.
def db_set(self):
db_url = 'mongodb://pyton:pyton@192.168.0.2:27017'
client = pymongo.MongoClient(db_url)
db = client.blog
collection = db.crypto
return collection
def article_parse(self):
url_lists: list = self.article()
collection = self.db_set()
for url in url_lists:
respon = self.response(url)
soup = self.soup(respon)
title = soup.find('div', {'id': 'primary'}).find('h1').text
url = url
texts = soup.find('div', {'class': 'entry-content'}).text.strip()
collection.update_many({}, {'$set':{'title': title, 'url': url, 'text': texts}}, upsert=True)