【发布时间】:2017-10-27 00:44:20
【问题描述】:
我想在发送删除 API 时将 deleted_at 时间戳添加到数据库。
deletedAt: {type: Date, default: Date.now}
在删除 API 中,我正在调用 model.collection.remove()
任何人都可以建议如何在删除记录后添加时间戳以及正在删除的 id
【问题讨论】:
我想在发送删除 API 时将 deleted_at 时间戳添加到数据库。
deletedAt: {type: Date, default: Date.now}
在删除 API 中,我正在调用 model.collection.remove()
任何人都可以建议如何在删除记录后添加时间戳以及正在删除的 id
【问题讨论】:
您可以在模型上编写 pre 方法
Schema.pre('remove', function (next) {
console.log("THIS ID", this._id);
var currentDate = new Date();
user.deletedAt= currentDate;
next();
});
User.find({_id: key}, function(err, books){
if (err)
throw err;
else {
books.forEach(function(book){
book.remove(function(err){
// the 'remove' pre events are emitted before this book is removed.
});
})
}
});
【讨论】: