What is the meaning `required` in mongoose Schema?
Here, these act as model for your project. So, required is used as validation and index is working as index over that field
Now you have two ways :
- either put validation over here in schemas/models
- or just manually create validation for form at frontend using
JS/Jquery
and then long route
Now your answers:
Name
is not compulsory to be filled in. That's why norequired
is put over there.- when there is mandatory to fill any value for that field. Then
required
is used in schemas. True/False
enables or disables the usage of validation over that field. If you are using false means filling in for that field isn't compulsion at all. But usingfalse
is considered a good practice.- Index is special data structure which are used for increasing performance during
read/search
operations. It increases the speed of operations and are stored in memory.
Why required is not declared for `name' - ?
Answer: When a field is mandatory to fill then in that case we mention it as required. So here "name" is not required or mandatory field.
Why `required' declared?
Answer: As mentioned above, When a field is mandatory to be filled then in that case we mention it as required.
What is select - true/false -means?
Answer: This means that it will not be returned by default in the data when you fetch the document. you can specify if this path should be included or excluded from query results by default.
Schema options
When the index - should declared any why?
Answer: Index should be declared when you are searching data on that field frequently so when you create indexing on that field in that case it do not search that field in all the collections it will search value for that field using index and will return result very quickly.
How indexes work in mongodb
whenever we have to validate the particular field, so we used required. required: true means you must fill that field. required: false means you may or may not fill that field, but its a good practice.