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 doc.deleteOne() doesn't run query middleware currently #13707

Merged
merged 1 commit into from Aug 8, 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
14 changes: 8 additions & 6 deletions docs/middleware.md
Expand Up @@ -97,12 +97,14 @@ Here are the possible strings that can be passed to `pre()`
All middleware types support pre and post hooks.
How pre and post hooks work is described in more detail below.

**Note:** Mongoose registers `updateOne` and
`deleteOne` middleware on `Query#updateOne()` and `Query#deleteOne()` by default.
This means that both `doc.updateOne()` and `Model.updateOne()` trigger
`updateOne` hooks, but `this` refers to a query, not a document. To register
`updateOne` or `deleteOne` middleware as document middleware, use
`schema.pre('updateOne', { document: true, query: false })`.
**Note:** Mongoose registers `updateOne` middleware on `Query.prototype.updateOne()` by default.
This means that both `doc.updateOne()` and `Model.updateOne()` trigger `updateOne` hooks, but `this` refers to a query, not a document.
To register `updateOne` middleware as document middleware, use `schema.pre('updateOne', { document: true, query: false })`.

**Note:** Like `updateOne`, Mongoose registers `deleteOne` middleware on `Query.prototype.deleteOne` by default.
That means that `Model.deleteOne()` will trigger `deleteOne` hooks, and `this` will refer to a query.
However, `doc.deleteOne()` does **not** fire `deleteOne` query middleware for legacy reasons.
hasezoey marked this conversation as resolved.
Show resolved Hide resolved
To register `deleteOne` middleware as document middleware, use `schema.pre('deleteOne', { document: true, query: false })`.

**Note:** The [`create()`](./api/model.html#model_Model-create) function fires `save()` hooks.

Expand Down