Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make autoIndex syncIndex instead of createIndex #14602

Open
2 tasks done
GustavoOS opened this issue May 17, 2024 · 4 comments
Open
2 tasks done

Make autoIndex syncIndex instead of createIndex #14602

GustavoOS opened this issue May 17, 2024 · 4 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Milestone

Comments

@GustavoOS
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

馃殌 Feature Proposal

autoIndex will check existing indexes instead of re-creating indexes everytime the app runs

Motivation

Mongoose docs explains that autoIndex is not advised to production environments because it creates indexes everytime the app runs. This makes the process of introducing indexes tricky in production. Mongoose offers synIndexes, but this has to be called individually for each model.

Example

No response

@GustavoOS GustavoOS added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class labels May 17, 2024
@vkarpov15
Copy link
Collaborator

You can use Connection.prototype.syncIndexes(): https://mongoosejs.com/docs/api/connection.html#Connection.prototype.syncIndexes() . For example, await mongoose.connection.syncIndexes()

We won't make autoIndex use syncIndexes by default for now, because that's potentially expensive and risky (unintentional deletion of index can cause serious performance issues) but we can add an option to make autoIndex use syncIndexes()

@vkarpov15 vkarpov15 added this to the Parking Lot milestone Jun 2, 2024
@vkarpov15
Copy link
Collaborator

@hasezoey @AbdelrahmanHafez what do you think, is it worthwhile to add a separate option to make autoIndex use syncIndexes() under the hood? I'm not entirely convinced because await mongoose.connection.syncIndexes() is already a one-liner, the option wouldn't make things particularly cleaner.

@hasezoey
Copy link
Collaborator

hasezoey commented Jun 3, 2024

@hasezoey @AbdelrahmanHafez what do you think, is it worthwhile to add a separate option to make autoIndex use syncIndexes() under the hood? I'm not entirely convinced because await mongoose.connection.syncIndexes() is already a one-liner, the option wouldn't make things particularly cleaner.

Personally, i dont mind if there would be a extra option to use syncIndexes instead, as long as it is not the default for autoIndex or at least wait until the next major version for that.
Maybe a different option like syncIndex, or allow autoIndex to be a string to be set to bool | 'createIndexes' | 'syncIndexes' (where bool is backwards compatible to createIndexes and any non-proper value either give a error, or a warning and treat it as false; maybe also check it lowercased?), which i think would be cleaner than doing a nested object or another option which would be mutually exclusive.

@GustavoOS
Copy link
Author

GustavoOS commented Jun 3, 2024

Well, the semantics of auto index is "mongoose will take care of index for you". But this isn't true simply because this option should never be used in production (as stated in current docs) as it tries to recreate indexes as many times as possible.
The purpose of this feature request is to make autoIndex actually useful and meaningful, in a way it can be used in production.

As this should not be used in production, the change of behaviour would not break previous code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

No branches or pull requests

3 participants