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

docs(middleware): clarify that query middleware applies to document by default #13734

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,28 @@ schema.pre('deleteOne', { query: true, document: false }, function() {
});
```

Mongoose also has both query and document hooks for `validate()`.
Unlike `deleteOne` and `updateOne`, `validate` middleware applies to `Document.prototype.validate` by default.

```javascript
const schema = new mongoose.Schema({ name: String });
schema.pre('validate', function() {
console.log('Document validate');
});
schema.pre('validate', { query: true, document: false }, function() {
console.log('Query validate');
});
const Test = mongoose.model('Test', schema);

const doc = new Test({ name: 'foo' });

// Prints "Document validate"
await doc.validate();

// Prints "Query validate"
await Test.find().validate();
```

<h2 id="notes"><a href="#notes">Notes on findAndUpdate() and Query Middleware</a></h2>

Pre and post `save()` hooks are **not** executed on `update()`,
Expand Down