mongoose 默认会将嵌套对象中声明的子字段(如 `comments.text`)视为独立路径并强制校验,即使父字段未设 `required: true`;正确做法是显式定义 `type` 为一个对象结构,并将 `required` 明确设为 `false` 或省略。
在 Mongoose 中,嵌套字段(如 comments)的 Schema 定义方式直接影响验证行为。你当前的写法:
comments: {
text: { type: String },
author: { type: mongoose.Types.ObjectId, ref: "User" },
}
看似未设 required,实则触发了 Mongoose 的隐式行为:当嵌套对象以“扁平属性形式”声明(即不包裹在 type: { … } 内),Mongoose 会自动将每个子键(comments.text、comments.author)注册为独立的 schema 路径,并默认启用 required: true —— 即使你未显式声明。这正是报错 Path comments.author is required 的根本原因。
✅ 正确写法是显式指定 type 为一个对象结构,并将 required 明确控制在父级:
comments: {
type: {
text: { type: String },
author: {
type: mongoose.Types.ObjectId,
ref: "User"
}
},
required: false // ✅ 显式设为 false(也可省略,默认即 false)
}
? 注意:required: false 可省略,因为 required 默认值就是 false;但显式写出更清晰,避免团队误解。
完整修正后的 Schema 片段如下: