有没有办法在mongoose模式中有选择地应用时间戳?

时间:2017-08-17 14:04:11

标签: mongodb mongoose mongoose-schema

我目前正在设计一个猫鼬模式。该架构适用于博客评论,如下所示:

removeColumn

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

1 个答案:

答案 0 :(得分:1)

我相信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
});
相关问题