有没有办法将created_at和updated_at字段添加到mongoose模式,而不必在每次调用新的MyModel()时都传递它们?
created_at字段将是一个日期,仅在创建文档时添加.每当在文档上调用save()时,updated_at字段将使用新日期进行更新.
我在我的架构中尝试了这个,但除非我明确地添加它,否则该字段不会显示:
var ItemSchema = new Schema({
name : { type: String, required: true, trim: true }
, created_at : { type: Date, required: true, default: Date.now }
});
Run Code Online (Sandbox Code Playgroud)