Reputation: 1147
I have a mongo db collection that is updated (a new record is added) once a second. Will it suffice to call ensure_index ONCE on the index attribute or do I have to call it repeatedly - for example after each document is added?
I understand that ensure_index looks at the last time it was invoked and potentially re-indexes the collection. What I fail to understand is why this should be necessary in my case ( or rather I don't know if this is necessary in my case). If the order of indexing or index attribute is not changing over time, do I need to repeatedly call ensure_index once for each time a new document is added?
Upvotes: 0
Views: 120
Reputation: 52020
PyMongo's ensure_index
is not exactly the same thing as the Mongo Shell ensureIndex
.
In pre-3.0 versions of PyMongo, ensure_index
will try to create an index only if it is not already in the drivers's cache. That later is a a per-driver's instance in memory cache (whose TTL in user configurable). So this could lead to confusing situations where one client might drop an index, and an other client believes it still exists.
Please note that starting from 3.0, ensure_index
is deprecated and you should now always use create_index
instead -- and let it fail elegantly if the index is already existing.
Upvotes: 1
Reputation: 4523
You should call it just once. In fact this method is imdepotent, which means 'once you created an index on attribute(s), repeating the same endureIndex will have no effect'
Upvotes: 1