Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)

My test schema with the mongoose in nodejs contains the following fields:

testschema = mongoose.Schema({

         name:{

    type:String,

    required:true,

    unique:true

  },

  image:{

    type:String,

    required:true

  },

  category:{

    type:String

  },

});

I want to make the category field in my schema as optional and if the user didn't provide any value to that optional field then it should be set to blank by default. How can I make the category field optional?

1 Answer

0 votes
by (107k points)

In MongoDB, you just have to set the default field as ''. Refer to the following code:

category: {

    type: String,

    default: ''

}

If the user didn't provide the value to the category value then by default it will set to '' which means blank. 

Related questions

+1 vote
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...