Model with the Mongoose schema

Now, let's create our model, using the Mongoose schema to map our speakers on MongoDB.

// Import the Mongoose module.
var mongoose     = require('mongoose'),
var Schema       = mongoose.Schema;

// Set the data types, properties and default values to our Schema.
var SpeakerSchema   = new Schema({
    name:           { type: String, default: '' },
    company:        { type: String, default: '' },
    title:          { type: String, default: '' },
    description:    { type: String, default: '' },
    picture:        { type: String, default: '' },
    schedule:       { type: String, default: '' },
    createdOn:      { type: Date,   default: Date.now}
});
module.exports = mongoose.model('Speaker', SpeakerSchema);

Note that on the first line, we added the Mongoose module using the require() function.

Our schema is pretty simple; on the left-hand side, we have the property name and on the right-hand side, the data type. We also we set the default value to nothing, but if you want, you can set a different value.

The next step is to save this file to our project folder. For this, let's create a new directory named server; then inside this, create another folder called models and save the file as speaker.js. At this point, our folder looks like this:

Model with the Mongoose schema

Tip

The README.md file is used for GitHub; as we are using the Git version control, we host our files on GitHub.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.187.165