In a Python script using PyMongo, an index is created for a collection using the line
myCollection.create_index('Datetime', unique=True)
However this throws an error the next time the script is executed because the index already exist.
Question: Is there a way to check for the existance of an index before deciding whether to call create_index
?
In PyMongo 3.6.0 and later, calling create_index
will not recreate the index if it already exists and will not throw an error. The index creation will just be ignored if the index already exists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With