Pymong adds a unique index pymongo. Errors. Duplicatekeyerror

Add index:

db.test1.createIndex({name: 1},{unique: true});   # Name is the key word   Unique: unique index
db.test1.getindexes ()    # Query index

Appears when Python inserts duplicate data   pymongo.errors.DuplicateKeyError   Wrong.

try:
  db.test1.save(post)
except pymongo.errors.DuplicateKeyError:
  print(111)

Read More: