我正在构建提供有关笔记本电脑信息的API。我为此使用node和mongoDB以及mongoose。这是我的架构-
const productSchema = mongoose.Schema(
{
name: {
type: String,
required: [true, 'A product must have a name'],
unique: true,
},
},
{
toJSON: {
virtuals: true,
},
toObject: {
virtuals: true,
},
});
我有一个名字字段。我还想要一个“ specs”字段,该字段应该是一个对象。我想为“ specs”字段的对象中的属性定义规则。我希望规格表中的内容是-
specs: {
memory: "16gb",
storage: "512gb",
"processor": "i7 2.2ghz"
}
因此,我想在“ specs”对象中为“内存”,“存储”,“处理器”定义规则
如果按规则表示模式对象规则,则可能会执行以下操作:
specs: {
type: Object,
default: {
memory: {
type: String,
default: null
},
storage: {
type: String,
default: null
},
processor: {
type: String,
default: null
}
}
}
您可以根据需要添加更多规则。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句