-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[Feature] Schema#removeIndex(...) #11547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Turns out The workaround that I found for removing all indexes: const cartSchemaClone = cartSchema.clone();
cartSchemaClone.set('excludeIndexes', true); // ignore _all_ indexes
const checkoutSchema = new Schema({ cart: cartSchemaClone }); This fixes my issue, but would become a problem once I need to add some indexes. I think we need to provide: |
@vkarpov15 Admittedly, this is a hacky solution that I can see causing issues for users thinking that this identifier representing something on MongoDB server, but I am not sure what other way we can reliably identify indexes in our schemas. Another way would be to identify using the object reference by strict equality, that way we make it the dev's responsibility to find the desired index from |
We should support both schema.removeIndex('my-index-name') and schema.removeIndex({ status: 1 }) . The latter should remove any index that is deep equal to the given spec. Also schema.clearIndexes() should remove all indexes defined on the schema. Shouldn't have any impact on indexes in MongoDB |
I agree that removing an index from schema shouldn't touch MongoDB. For deep equality check, I think we should check both the index definition and the options. Also, deep equality check should treat objects with the same properties in different order as equal. For string index names, I am not sure if they're unique or not, one other concern, AFAIK people can set a custom index name, if we wanna match with the index name, does that mean we'll need to fetch indexes from the database? Needing to fetch indexes means we'll have to tie the index removal with database connections. I think supporting string index names, knowing that users can set custom indexes names, will introduce more issues than the feature requires, deep equality check should do the trick. |
gh-11547 Can remove and clear indexes on the schema
I came across a case where I cloned a schema, and would like to reuse it in another model without the indexes.
The issue is that the cloned schema includes its indexes, and there is no official way to remove indexes, I had to access
schema._indexes
which doesn't work with TS.The text was updated successfully, but these errors were encountered: