A `syncIndexes()` API for PyMongo.
Implement a method on the collection object, syncIndexes
, that takes a list of IndexModels (similar to the existing createIndexes
method) and ensures that the indexes in the collection is exactly the same as those specified in the list.
Similar features already exist in Mongoose: https://mongoosejs.com/docs/api/model.html#Model.syncIndexes().
Why this should be in the library: It is incredibly difficult and error prone to implement this manually (which is what we're doing right now):
We currently do this along with some other Mongo sanity checks when our server starts up. Since this requires multiple
createIndexes
,getIndexes
, anddropIndexes
calls, and to ensure isolation we use a distributed lock to ensure only one server is doing this at the same time. If this is implemented natively in Mongo or the driver as a one-shot operation, this lock wouldn't be needed.There are some particular nasty ordering issues. For instance, on a sharded collection, if an index exists in the DB as the shard key, and in our code we want to add a new property to the index (say making it unique), we would need to
- Create a new dummy index which is {"shardkey": ..., "dummykey": 1}.
- Delete the existing index.
- Recreate the index with the property set to what we want.
- Delete the dummy index. Again, if this is a native API, developers wouldn't need to think about these issues.
