const articleSchema = new mongoose.Schema({
title: {
type: String,
maxlength: 20,
minlength: 4,
required: [true, '请填写文章标题'],
},
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: [true, '请传递作者']
},
publishDate: {
type: Date,
default: Date.now
},});
参考GPT和自己的思路:
根据你提供的代码,如果你在MongoDB中插入一个文档时,publishDate字段不提供任何值,MongoDB会使用默认值Date.now(),该值代表当前时间。如果在插入文档时没有提供publishDate值,并且默认时间没有生效,可能是因为插入代码中有修改publishDate值的操作,或者是因为插入时出现了错误或异常导致默认时间没有生效。你可以检查插入文档的代码和错误日志以解决此问题。