简体   繁体   中英

object as property mongoose mongodb

I need a property in a mongoose model that is as following:

customRanks should have different keys and different values for each key

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

You can defined custome rank schema and use this as a object if you want to save data in specific format for customRanks fields

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

If you want static schema and have specific fields for your custome rank object

// 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

If you are looking for validation without key specifically. Then you may need to validate data before insert/create/update using javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM