DEV Community

loading...

Step 3: Create Schemas

hajarnasr profile image Hajar | هاجر ・3 min read

A Schema in Mongoose is like a mold that we use to shape our model, to define what type of properties that model must have.

If you define a Schema with properties of String, Boolean, and Number, then your model data has to be of types String, Boolean, and Number.

Let us make two Schemas for the User and Post models we defined in the first step.

// models/User.js

const mongoose = require("mongoose")
// use mongoose Schema
const Schema = mongoose.Schema;

// Create a new Schema
const UserSchema = new Schema({
    // 1 
    username: {
         type: String, 
         default:"Anonymous",
        },
    // 2    
    email: {
        type: String,
        required: true,
        unique: true
    }
});

Enter fullscreen mode Exit fullscreen mode

Here, UserSchema is a simple Schema that has only two properties:
1- username: It has to be string, and if it is absent, "Anonymous" will be stored in the database instead.
2- email: It also has to be string, but unlike username, required, which means you can't save a user document to the database without adding their email first. Also, that email has to be unique.

// models/Post.js

const mongoose = require("mongoose")

// use mongoose Schema
const Schema = mongoose.Schema;

// Create a new Schema for Post model
const PostSchema = new Schema({
    // 1 
    title: String,
    // 2    
    content: {
        type: String,
        required: true,
        trim: true,
        minlength: 5
    },
    // 3
    created_at: {
        type: Date,
        default: Date.now
    },
    // 4
    author: {
        type: Schema.Types.ObjectId,
        ref: "User"
    }
});
Enter fullscreen mode Exit fullscreen mode

Here, we have four properties for PostSchema:
1- title: It has to be string. It's not required and doesn't have a default value, so when it's absent, it doesn't get to be stored in the database at all.
2- content: It has to be string and is required. It has to be at least five characters long and trimmed before saving to the database.
3- created_at: It has to be Date, and its default value is Date.now.
4- author: That is a bit different than other types here.

Above, author is of type Schema.Types.ObjectId, which means that it's a link/ref to a user document in the database. So, instead of writing all the information about author in both User and Post models, we save the author's _id in the Post model as a ref to the actual user document.

We can create as many Schemas as we want for our app. Here, we could add CommentSchema or LikeSchema. And with every new Schema, we could add { type: Schema.Types.ObjectId, ref: "<Model>" } properties to link to objects in other models.

Now that we have finished with writing our Schemas, it's time to go to the next step and make models of these Schemas.

Step 4: Make models of these Schemas

It takes only one line to make a model of any Schema in Mongoose. All you need to do is choose a name for your model and pass that name with your Schema to mongoose.model.

// models/User.js 
...
// make User model by using UserSchema
module.exports = User = mongoose.model("User", UserSchema);
Enter fullscreen mode Exit fullscreen mode
// models/Post.js file
...
// make Post model by using PostSchema
module.exports = Post = mongoose.model("Post", PostSchema);
Enter fullscreen mode Exit fullscreen mode

Now we have two models to use to store our data into the database.

Refrences
freeCodeCamp
the mongoosejs docs.
MDN
code.tutsplus

Discussion

pic
Editor guide