猫鼬模式

时间:2020-10-26 07:35:59

标签: javascript node.js mongodb mongoose mongoose-schema

我正在构建一个提供有关笔记本电脑信息的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"
}

因此,我想为“规范”对象中的“内存”,“存储”,“处理器”定义规则

1 个答案:

答案 0 :(得分:2)

如果按规则表示的是架构对象规则,则可能会执行以下操作:

presentingViewController

您可以根据需要添加更多规则。