[英]Mongoose deleteMany in pre-hook, how to access all documents that will be deleted?
I have this code:我有这个代码:
postSchema.pre('deleteMany', async function (next: NextFunction) {
try {
console.log(this);
return next(); // normal save
} catch (error) {
return next(error);
}
});
console.log
is giving a query
object
. console.log
给出query
object
。 Are documents available somewhere in the query
?文档在query
中的某处可用吗?
query in deletemany
Located at _conditions
key of this
,so you can do like this in Post Model for example:在deletemany
中查询位于this
的_conditions
键,因此您可以在 Post Model 中这样做,例如:
postSchema.pre('deleteMany', async function (next: NextFunction) {
try {
let deletedData =await Post.find(this._conditions).lean()
console.log(deletedData );
return next(); // normal save
} catch (error) {
return next(error);
}
});
let Post= mongoose.model("Post", userSchema);
module.exports = Post
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.