Hello,
I have a microservice written in Java which has a mongodb database. I would like to manage (a bit like with terraform) a list of indexes that when the service is deployed, the indexes on the database match exactly what is declared in the service.
I have noticed that createIndex is idempotent so if I try to create the same index on startup there is no impact. The problem is that if I want to remove an index, I have to add a call to removeIndex on startup which gets called every time. Or I can loop through the indexes and try to inspect the documents to work out if it is the same but there are lots of different options for ttl indexes to inspect etc. The documents returned by getIndexes don’t match those that we use to create so there we can’t use an equality check and the IndexOptions is a separate argument that we pass to createIndex, it is not in the same document.
Is there a recommended way to manage this? In my head, ideally we could provision the indexes via terraform that checks the exact state. If not, do I need to develop some algorithm that loops through the indexes on the collection, removes any that don’t match and then create any new ones.
Thanks for any guidance