Writing a schema

Let's write the schema for a User in our MongoosePM application.

The first thing we have to do is declare a variable to hold the schema. I recommend taking the object name (for example, user or project) and adding Schema to the end of it. This makes following the code later on super easy.

The second thing we need to do is create a new Mongoose schema object to assign to this variable. The skeleton of this is as follows:

var userSchema = new mongoose.Schema({ });

We can add in the basic values of name, email, and createdOn that we looked at earlier, giving us our first user schema definition.

var userSchema = new mongoose.Schema({
  name: String,
  email: String,
  createdOn: Date
});

Modifying an existing schema

Suppose we run the application ...

Get Mongoose for Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.