Skip to content

Is there a way to disable update and create functions in a mongoose scope? #13429

Answered by vkarpov15
vexCoder asked this question in Q&A
Discussion options

You must be logged in to vote

Plugins and middleware. Below is a general idea.

mongoose.plugin(schema => {
  schema.pre('save', () => { throw new Error('save not allowed'); });
  schema.pre(['updateOne', 'updateMany', 'findOneAndUpdate', 'findOneAndDelete', 'findOneAndReplace', 'deleteOne', 'deleteMany', 'replaceOne'], () => {
    throw new Error('Writes not allowed');
  });
});

Another approach would be to define a separate user on your database that only has readonly privileges, and use that user for scripting.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by vexCoder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants