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 with this for a while, and then decide that we want to record the last time each user logged on, and the last time their record was modified. No problem!
We don't have to refactor the database or take it offline while we upgrade the schema...