Skip to Main Content

MongoByte MongoDB Logo

Welcome to the new MongoDB Feedback Portal!

{Improvement: "Your idea"}
We’ve upgraded our system to better capture and act on your feedback.
Your feedback is meaningful and helps us build better products.

Status Submitted
Created by Guest
Created on May 26, 2025

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`, and `dropIndexes` 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 1. Create a new dummy index which is {"shard_key": ..., "dummy_key": 1}. 2. Delete the existing index. 3. Recreate the index with the property set to what we want. 4. Delete the dummy index. Again, if this is a native API, developers wouldn't need to think about these issues.