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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hook to collection creation #7893

Closed
abouroubi opened this issue Jun 12, 2019 · 8 comments · Fixed by #14358
Closed

Hook to collection creation #7893

abouroubi opened this issue Jun 12, 2019 · 8 comments · Fixed by #14358
Labels
discussion If you have any thoughts or comments on this issue, please share them! new feature This change adds new functionality, like a new method or class
Milestone

Comments

@abouroubi
Copy link

Do you want to request a feature or report a bug?
Feature
What is the current behavior?
I want to execute some custom commands on collection creation.
Is there a way to hook to or to override the automatic collection creation by mongoose ?
If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

@vkarpov15
Copy link
Collaborator

By default, Mongoose doesn't create collections automatically unless you enable autoCreate. MongoDB automatically creates collections though.

We can add middleware for Model.createCollection(), so you could do schema.pre('createCollection'). Would that help?

@vkarpov15 vkarpov15 added new feature This change adds new functionality, like a new method or class discussion If you have any thoughts or comments on this issue, please share them! labels Jun 16, 2019
@vkarpov15 vkarpov15 added this to the Parking Lot milestone Jun 16, 2019
@abouroubi
Copy link
Author

That would be helpful yes, thank you.

Will it be possible to override the creation process or just add pre creation commands ?

@vkarpov15
Copy link
Collaborator

@abouroubi just add pre and post creation commands. Is that enough for your use case?

@vkarpov15 vkarpov15 reopened this Nov 20, 2023
@FortesJos
Copy link

We'd love to see a schema.pre('createCollection')-hook (or connection.pre('createCollection').) As far as I can see it hasn't been implemented currently

@vkarpov15
Copy link
Collaborator

@FortesJos what's your use case for wanting a pre('createCollection') hook?

@FortesJos
Copy link

The use case is preventing (accidental) creation of new collections.

@vkarpov15
Copy link
Collaborator

The problem is that, if you attempt to insert a document into a nonexistent collection, MongoDB will create the collection automatically. So there's no good way to prevent accidentally deleting a collection in all cases. Would that be a deal breaker for you?

@FortesJos
Copy link

That is not an issue. We'll be using it to stop running the app initially. When inserting would be an issue later on, these can be caught via the usual hooks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion If you have any thoughts or comments on this issue, please share them! new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants