【发布时间】:2018-08-11 00:51:33
【问题描述】:
当我在 mongoose 中有一个模式,里面有这样的嵌套模式时
const Sub = Schema({
foobar: String,
})
Sub.pre('remove', function(next) { ... next() })
const Main = Schema({
foo: String,
bar: [Sub],
})
Main.pre('remove', function(next) { ... next() })
当我删除 Main 文档时,主文档和子文档都会调用 remove-middleware get。
但是当我只是删除一个子文档时,没有调用 remove-hook get。例如:
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
这是应该的吗?如果是这样 - 我如何编写一个在删除子文档而主文档不删除时运行的中间件?
【问题讨论】: