Mongoose make Object required 1

Mongoose make Object required

const extraDataSchema = new mongoose.Schema({
  brand: {
    type: String,
    required: true,
    minlength: 1,
    maxlength: 255
  },
  quantity: {
    type: Number,
    required: true,
    minlength: 1,
    maxlength: 10
  }
});

const productSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    minlength: 1,
    maxlength: 255
  },
  extraData: {
    type: extraDataSchema, required: true
  }
});

Here is what the above code is Doing:
1. We’re creating a new schema called extraDataSchema.
2. We’re creating a new schema called productSchema.
3. We’re adding a new property called extraData to productSchema.
4. We’re setting the type of extraData to extraDataSchema.
5. We’re setting the required property of extraData to true.

Now, let’s create a new product document:

Similar Posts