0

I write this script for insert a doc into Mongodb if not duplicated

import tldextract
from pymongo import MongoClient

client = MongoClient()
db = client.my_domains
collection = db.domain

with open('inputcut.csv', 'r') as f:
  for line in f:
  ext = tldextract.extract(line)
  domain = {"domain":ext.registered_domain}
  collection.update(domain,{'upsert':True})

When I run the script, no domains are inserted into the database. I would like to insert a domain if it is not yet present in mongodb. If the domain is already present, we do not insert it and we go to the next one ...

Thank you in advance for your help.

LionelF
  • 137
  • 1
  • 1
  • 11

1 Answers1

0

collection.update expects 3 arguments - the query, the update and the options. Since upsert should be in the options, rewrite the call as follows:

collection.update(domain, {$set: domain}, {'upsert':True})
matanso
  • 1,284
  • 1
  • 10
  • 17