Mongoose deleteMany在pre-hook中,如何访问所有将要删除的文档?

Chr*_* K. 1 javascript mongoose mongodb node.js

我有这个代码:

postSchema.pre('deleteMany', async function (next: NextFunction) {
  try {
    console.log(this);
    return next(); // normal save
  } catch (error) {
    return next(error);
  }
});
Run Code Online (Sandbox Code Playgroud)

console.log正在给予query object. 文档可以在 中的某个地方找到吗query

Moh*_*adi 6

查询deletemany位于 的_conditionsthis,因此您可以在帖子模型中执行以下操作,例如:

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
Run Code Online (Sandbox Code Playgroud)