db.collection.ensureIndex()
Definition
db.collection.
ensureIndex
(
keys, options)
Deprecated since version 3.0.0: db.collection.ensureIndex()
is now an alias fordb.collection.createIndex()
.
Creates an index on the specified field if the index does not already exist.
But in pymongo :
Unlike :meth:`create_index`, which attempts to create an index unconditionally, :meth:`ensure_index` takes advantage of some caching within the driver such that it only attempts to create indexes that might not already exist. When an index is created (or ensured) by PyMongo it is "remembered" for `cache_for` seconds. Repeated calls to :meth:`ensure_index` within that time limit will be lightweight - they will not attempt to actually create the index.