Introducing schemas
So what is a schema? At its simplest, a schema is a way to describe the structure of data. Typically this involves giving each piece of data a label, and stating what type of data it is, for example, a number, date, string, and so on.
As we have already seen in Chapter 1, Introducing Mongoose to the Technology Stack, a schema definition in Mongoose is a JSON object. In the following example, we are creating a new Mongoose schema called userSchema
. We are stating that a database document using this schema will have three pieces of data, which are as follows:
name
: This data will contain a stringemail
: This will also contain a string valuecreatedOn
: This data will contain a date
The following is the schema definition:
var userSchema = new mongoose.Schema({ name: String, email: String, createdOn: Date });
Field sizes
Note that, unlike some other systems there is no need to set the field size. This can be useful if you need to change the amount of data stored in a particular...