2017-08-17 53 views
0

我目前正在设计猫鼬纲要。该模式适用于博客评论,如下所示:有没有办法在猫鼬模式中选择性应用时间戳?

new mongoose.Schema({ 
    commentedOn: { 
    type: mongoose.Schema.Types.ObjectId, 
    required: true 
    }, 
    author: { 
    type: String, 
    required: true 
    }, 
    contents:{ 
    type: String 
    }, 
    points: { 
    type: Number, 
    default:0 
    }, 
    timestamps: true 
}) 

点数字段用于记录一条评论的投票。每次用户投票评论时,我都不想更改时间戳。有没有办法做到这一点?或者我应该将点域移出该模式吗?

回答

0

我认为timestamps应该通过second argument of the schema

关于你的问题,我认为这样做的唯一方法是不要使用timestamps并明确声明你的时间戳字段,例如createdAtupdatedAt。无论何时保存或更新,都将根据具体情况明确设置updatedAt字段(或不)。

new mongoose.Schema({ 
    commentedOn: { type: mongoose.Schema.Types.ObjectId, required: true }, 
    author: { type: String, required: true }, 
    contents: String, 
    points: { Number, default: 0 }, 
    createdAt: { type: Date, default: Date.now }, 
    updatedAt: Date 
});