2017-08-17 57 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 
});