Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning `required` in mongoose Schema?

I am writing a mongoose schema, and I would like to understand the properties of the same.

Here is my schema:

var UserSchema = new Schema({
        name: String,
        username: { type: String, required: true, index: { unique: true }},
        password: { type: String, required: true, select: false }
});
  1. Why required is not declared for `name' - ?
  2. Why required declared?
  3. What is select - true/false -means?
  4. When the index - should declared any why?
like image 968
user2024080 Avatar asked Oct 05 '16 10:10

user2024080


People also ask

What is require in Mongoose?

The require('mongoose') call above returns a Singleton object. It means that the first time you call require('mongoose') , it is creating an instance of the Mongoose class and returning it.

Is Mongoose required for MongoDB?

Mongoose is an object document modeling (ODM) layer that sits on top of Node's MongoDB driver. If your coming from SQL, it's similar to an ORM for a relational database. While it's not required to use Mongoose with the Mongo, here are four reasons why using Mongoose with MongoDB is generally a good idea.

Is schema necessary for Mongoose?

MongoDB is not schema-less. It's got flexible schema - there is a big difference. it's a matter of taste, Mongoose lets you have a typed schema with validations, if you use the driver you don't get that and will have to roll your own.

What is the need of Mongoose .how it is useful for validation?

Mongoose gives us a set of useful built-in validation rules such: Required validator checks if a property is not empty. Numbers have min and max validators. Strings have enum , match , minlength , and maxlength validators.


1 Answers

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

like image 110
Sachin Avatar answered Oct 24 '22 02:10

Sachin