Skip timestamps middleware for certain updates in Mongoose

前端 未结 3 1690
北恋
北恋 2021-01-04 16:14

My application uses Mongoose and has a schema that uses the timestamps option:

var fooSchema = new Schema({
    name: String,
}, {
    timestamps: true,
});
         


        
3条回答
  •  猫巷女王i
    2021-01-04 17:02

    What i can get is you are automatically updating the dateTime of updated_at field. You must be passing a default value for the updated_at in your schema, just remove that default field.

    For example.

    var fooSchema = new Schema({
        name: String,
    }, {
        updated_at:{
             type: Date,
             default: Date.now
        }
    });
    mongoose.model('Foo', fooSchema);
    

    Remove the default field from updated_at and your schema will look like this.

    var fooSchema = new Schema({
        name: String,
    }, {
        updated_at: Date
    });
    mongoose.model('Foo', fooSchema);
    

提交回复
热议问题