【问题标题】:Mongoose timestamps - Force specific createdAtMongoose 时间戳 - 强制特定于 createdAt
【发布时间】:2025-12-17 22:20:09
【问题描述】:

我正在使用 mongoose 和 mongoose 时间戳插件,但有时我想添加一个具有特定 createdAt 的对象,而不是现在,有什么方法可以覆盖时间戳?

var timestamps = require('goodeggs-mongoose-timestamps');
var Schema = mongoose.Schema;

var ClientSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    removed: {
        type: Boolean,
        default: false
    },
});

ClientSchema.plugin(timestamps);

module.exports = mongoose.model('Client', ClientSchema);

【问题讨论】:

    标签: mongoose


    【解决方案1】:

    你为什么不在你的架构中放置一个 createdAt 而不是使用插件?而且您可以随时覆盖您的 createdAt。

    createdAt: { type: Date, default: Date.now }
    

    【讨论】: