When I have a schema in mongoose with nested schemas inside like this
const Sub = Schema({
foobar: String,
})
Sub.pre('remove', function(next) { ... next() })
const Main = Schema({
foo: String,
bar: [Sub],
})
Main.pre('remove', function(next) { ... next() })
When I remove a Main
document, the remove-middleware get's called for both the Main and the Sub documents.
But when I just remove a subdocument, no remove-hook get's called. For Example:
const main = await new Main({
foo: 'test',
bar: [{
foobar: 'test'
}),
}).save()
await main.bar[0].remove() // or bar.pull()
// pre/post remove hooks on Subdocument not called
Is this how it's supposed to be? And if so - how can I write a middleware that runs whenever a subdocument is removed while the main document is not?