Gianmarco
Gianmarco

Reputation: 832

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,
});

Upvotes: 0

Views: 411

Answers (1)

Ashok
Ashok

Reputation: 2932

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.

Upvotes: 1

Related Questions