我有一个mongoose Schema,它有一个字段created
来保存文档的创建时间
var Document = new Schema({
created: {type: Date, default: Date.now},
lastModified: {type: Date},
title: {type: String, default: '', trim: true}
...
});
我想使用mongoosetimestamp
功能,去掉created和lastModified。
var Document = new Schema({
title: {type: String, default: '', trim: true}
...
}, {timestamps: true});
然后,我显然需要为所有现有文档复制created到createdAt和lastModified到updatedAt。
这样做的正确方法是什么?
从Schema中删除字段,然后简单地重命名字段。
await Document.updateMany({}, { $rename: { 'created':'createdAt' } }, { strict: false, timestamps: false });
await Document.updateMany({}, { $rename: { 'lastModified' : 'updatedAt' } }, { strict: false, timestamps: false });