Make mongoose string schema type default value as blank and make the field optional Make mongoose string schema type default value as blank and make the field optional mongoose mongoose

Make mongoose string schema type default value as blank and make the field optional


What you most likely need here is the default value set.

category: {    type: String,    default: ''}

This makes the field somewhat optional, because if you don't set it, it defaults to ''

Setting defaults on Update

As mentioned by user whoami, mongoose only sets defaults on insert.

If you are using mongoose 4.x and up and MongoDB 2.4.0 and up you can opt-in to setting default values on update too.

Mongoose docs: The setDefaultsOnInsert option


Another way to overcome this current limitation on the new inserts is just to place a default value when you are doing the post request. As in this sample code:

var category = req.body.category || 0;

That assuming the user hasn't touched the field, therefore its value is null by default.