【问题标题】:MongoDB / Mongoose timestamps not updatingMongoDB / Mongoose 时间戳未更新
【发布时间】:2017-01-22 13:38:50
【问题描述】:

架构:

var schema = new Schema({...}, {
    timestamps: true,
    id: false,
    toJSON: {
        virtuals: true,
    },
    toObject: {
        virtual: true,
    }
});
schema.virtual('updated').get(function () {
    if(typeof this.updatedAt === "undefined" && typeof this.createdAt === "undefined") return "";
    var updated = (typeof this.updatedAt === "undefined") ? this.createdAt : this.updatedAt;
    return "Updated "+moment(updated).fromNow();
});

此代码之前最近工作 - 特定实例的 updatedAt 出现在 8 月 24 日,但是对文档的任何新编辑不会更新时间戳。 p>

感觉我在这里遗漏了一些非常愚蠢的东西。

【问题讨论】:

  • 你能检查一下 this.updatedAt 的类型吗?
  • @abdulbarik typeof League.updatedAt => 对象
  • 我复制粘贴了您的代码并在我的服务器上运行,它在 mongoose 4.6.1 上运行得非常好,所以您可能在其他地方遗漏了一些东西。请提及您正在使用的猫鼬版本,或任何猫鼬插件。
  • @PuneetSingh Mongoose v.4.5.3。进一步测试表明 createdAt 创建成功并且是正确的,但是如果我们随后更新文档,则 updatedAt 的值不会改变。这是我调用编辑的地方:github.com/simon--poole/EventVODS/blob/master/app/routes/api/…
  • 看起来这是猫鼬时间戳中的一个已知问题。修复是在 3 天前完成的。也许您可以将 Mongoose 时间戳更新到最新版本并尝试一下?此链接中提供了有关修复的更多信息。 github.com/drudge/mongoose-timestamp/pull/37/commits/…

标签: node.js mongodb mongoose-schema


【解决方案1】:

您将objectString 进行比较,这就是为什么条件总是false

schema.virtual('updated').get(function () {
    if(typeof this.updatedAt === undefined && typeof this.createdAt === undefined) return "";
    var updated = (typeof this.updatedAt === undefined) ? this.createdAt : this.updatedAt;
    return "Updated "+moment(updated).fromNow();
});

试试这个,应该可以的

【讨论】:

  • 感谢您的回答,但问题似乎与该部分无关 - 问题在于 updatedAt 时间戳未在编辑时更新。
  • 你卡在哪个地方了?
  • Mongoose 时间戳,updatedAt,同时卡住 - 尽管我从那时起更新了文档。 updatedAt: "2016-08-24T12:28:39.842Z",。获取时间 since 的函数可以正常工作。
【解决方案2】:

可以尝试修改您的架构,例如:

var schema =new Schema({..}, 
           { timestamps: { createdAt: 'createdDate',updatedAt: 'updatedDate' } 
});

对于此架构,时间戳将在 save()update()findOneAndUpdate() 上更新。所以不需要schema.virtual('updated')...

进程 2

添加了createdDateupdatedDateDate,在您的架构中键入并使用架构插件 更新这些日期字段。

喜欢:

var mongoose = require('mongoose'),
    Schema   = mongoose.Schema,
    SchemaPlugin = require('../helpers/schemaPlugin');
  var schema =new Schema({..},
    createdDate: {
      type: Date,
      default: Date.now
    },
    updatedDate: {
      type: Date,
      default: Date.now
    }
  });

  schema.plugin(SchemaPlugin);

schemaPlugin.js 文件中:

module.exports = function(schema) {

  var updateTimestemps = function(next){
    var self = this;


    if(!self.createdAt) {
      self.createdDate = new Date();
      //or self.update({},{ $set: { createdDate : new Date(), updatedDate: new Date() } });
    } else {
      self.updatedDate= new Date();
      //or self.update({},{ $set: {updatedDate: new Date() } });
    }
    next();
  };

  schema.
    pre('save', updateTimestemps ).
    pre('update', updateTimestemps ).
    pre('findOneAndUpdate', updateTimestemps);
};

【讨论】:

  • 不错!你的第二个建议是我使用的。值得一提的是 Mongoose 文档 use the .set() method 在他们的示例中,例如:self.set({ updatedDate: new Date() });
【解决方案3】:

updatedAt 和 createdAt 都是在使用 mongoose 将新文档输入数据库时​​同时创建的,因此您检查 updatedAt 是否未定义是不合逻辑的,因为在创建新文档时两者将具有相同的值。

每当您使用猫鼬更新函数或 findByIdAndUpdate 或 findOneAndUpdate 时,updatedAt 的值都会自动更新。使用 Mongodb 客户端(如 mongochef 或 robomongo)直接检查 updatedAt 的值。

【讨论】:

  • 更新功能不会自动更新updatedAt。如果是这样,那么版本5.10.15 有一个错误。
【解决方案4】:

偶然发现同样的事情,发现如果在我使用findOneAndUpdate()(或update())更新我的对象时在我的对象上设置了属性updatedAt,它不会更新它。

在我的情况下,通过确保在更新之前未设置 updatedAt 来解决它:

delete thing.updatedAt;

Thing.findOneAndUpdate(
  { _id : thing._id },
  thing,
  function (err, result) {
   …

Credit to Valeri Karpov for his answer on Github.

【讨论】:

  • 好吧,我很久以前就放弃了这个问题,但这比我的解决方法要优雅得多 - A+ catch。
猜你喜欢
  • 2011-09-05
  • 2023-01-29
  • 1970-01-01
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
  • 2016-12-01
  • 1970-01-01
相关资源
最近更新 更多