【问题标题】:add deleted_at timestamp and id to database in nodejs在nodejs中将deleted_at时间戳和id添加到数据库
【发布时间】:2017-10-27 00:44:20
【问题描述】:

我想在发送删除 API 时将 deleted_at 时间戳添加到数据库。 deletedAt: {type: Date, default: Date.now}

在删除 API 中,我正在调用 model.collection.remove() 任何人都可以建议如何在删除记录后添加时间戳以及正在删除的 id

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您可以在模型上编写 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.
               });
           })
        }
    });
    

    请参阅doclink 了解更多详情

    【讨论】:

      猜你喜欢
      • 2012-04-04
      • 2015-12-03
      • 1970-01-01
      • 2019-10-07
      • 1970-01-01
      • 2010-10-26
      • 2014-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多