提问者:小点点

对象作为属性mongoose mongodb


我需要一个猫鼬模型的属性如下:

CustomRandard应该有不同的键,每个键的值也应该不同

const config = new Schema({
  id: { type: String, required: true, unique: true },
 //...other property 
  customRanks: Object,
});

共1个答案

匿名用户

您可以定义Custome rank schema,如果希望以特定格式为Customrank字段保存数据,则可以将其用作对象

const customRanksSchema = new Schema({
  key1: { type: Number, default: 0 },
  key2: { type: Number, default: 0 },
  key3: { type: String, default: '' }
})

如果您想要静态模式并为custome rank对象拥有特定字段

// if you want to save customeRanks as array of object
customRanks: [{ type: customRanksSchema }],
// if you want to save custom rank as a object
customRanks: customRanksSchema

如果您正在寻找没有关键字的验证,请特别指定。那么您可能需要在使用JavaScript插入/创建/更新之前验证数据。