如何找到id剛剛使用updateOneMongoose 中的 -post 掛鉤更新的文檔?它可能看起來像這樣:schema.post("updateOne", function (val) { if(val.nModified > 0) { /// "this" here refers to the schema, not the document... /// How can I get the _id field of the document that was modified? }}目前對于updateOneMongoose 中的 -post 掛鉤,this指的是查詢,而不是被修改的文檔本身。見這里:https ://mongoosejs.com/docs/middleware.html#types-of-middleware在查詢過程中可以找到id是否傳入,使用this.getQuery(),但這并不總是適用——我沒有使用該id字段進行查詢。因此,this.getQuery()不會返回給我我需要的數據。有沒有人有解決方案?如果可能的話,我寧愿將日志記錄部分保留在我定義架構的鉤子中,以使我的代碼更清晰,而不是在我運行該updateOne函數的代碼庫中。
在 Mongoose 的 updateOne Post Hook 中獲取文檔 ID
慕田峪7331174
2022-10-13 16:14:34