You can specify that a field must be included when you create a new instance of a Document
object for the model. By default, if you do not specify a field when creating a Document
instance, the object is created without one. For fields that must exist in your model, you add the required property when defining the schema. For example, to add an index, ensure uniqueness, and force the inclusion of the name
field in the collection, you use:
var schema = new Schema({
name: {type: String, index: 1, unique: true, required: true}
};
You can get a list of required fields on a Schema
object by using the requiredPaths()
method. For example:
schema.requiredPaths()