diff --git a/docs/middleware.md b/docs/middleware.md index d6c016108a7..67d1282f1c0 100644 --- a/docs/middleware.md +++ b/docs/middleware.md @@ -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 +}); +``` +

Pre

Pre middleware functions are executed one after another, when each