我在nodejs中有一个 Mongoose 的用户架构
userschema = mongoose.Schema({
org: String,
username: String,
fullname: String,
password: String,
email: String
});
除非有时我需要添加一些其他字段。
主要问题是:在 Mongoose 模式中可以有可选字段吗?
最佳答案
Mongoose 模式中的所有字段默认都是可选的(当然_id
除外)。
仅当您在其定义中添加 required: true
时,才需要该字段。
因此,将您的架构定义为所有可能字段的超集,然后将required: true
添加到所需的字段中。