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 });