架构:
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();
});
Run Code Online (Sandbox Code Playgroud)
此代码最近正在运行 - 对于特定实例,updatedAt将在8月24日出现,但对文档的任何新编辑都不会更新时间戳.
感觉我在这里错过了一些非常愚蠢的东西.