Step 3: Create Schemas
Tags: javascript, beginners, mongoose, express
Published: 2020-09-21 22:34:35

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. 
```javascript
// 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
    }
});

```
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. 
```javascript
// 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"
    }
});
```
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`. 

```javascript
// models/User.js 
...
// make User model by using UserSchema
module.exports = User = mongoose.model("User", UserSchema);
```

```javascript
// models/Post.js file
...
// make Post model by using PostSchema
module.exports = Post = mongoose.model("Post", PostSchema);
```
Now we have two models to use to store our data into the database.

**Refrences**
[freeCodeCamp](https://www.freecodecamp.org/learn/apis-and-microservices/mongodb-and-mongoose/)
[the mongoosejs docs](https://mongoosejs.com/docs/guide.html).
[MDN](https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/mongoose)
[code.tutsplus](https://code.tutsplus.com/articles/an-introduction-to-mongoose-for-mongodb-and-nodejs--cms-29527#:~:text=These%20documents%20are%20saved%20inside,via%20the%20Model's%20schema%20definition.)