Skip to content

Commit

Permalink
docs(middleware): added note about execution policy on subdocuments
Browse files Browse the repository at this point in the history
  • Loading branch information
lpizzinidev committed Nov 29, 2022
1 parent 66474c9 commit 4258642
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/middleware.md
Expand Up @@ -85,6 +85,26 @@ This means that both `doc.updateOne()` and `Model.updateOne()` trigger

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

**Note:** Query middlewares are not executed on subdocuments.

```javascript
const childSchema = new mongoose.Schema({
name: String
});

const mainSchema = new mongoose.Schema({
child: [childSchema]
});

mainSchema.pre('findOneAndUpdate', function () {
console.log('Middleware on parent document'); // Will be executed
});

childSchema.pre('findOneAndUpdate', function () {
console.log('Middleware on subdocument'); // Will not be executed
});
```

<h3 id="pre"><a href="#pre">Pre</a></h3>

Pre middleware functions are executed one after another, when each
Expand Down

0 comments on commit 4258642

Please sign in to comment.