We'll also be using middleware to have our model process something before it saves. In our case, we'll be creating a slug from the title.

// middleware -----// make sure that the slug is created from the name
eventSchema.pre('save',function(next){this.slug =slugify(this.name);next();});// function to slugify a namefunctionslugify(text){return text.toString().toLowerCase().replace(/\s+/g,'-')// Replace spaces with -.replace(/[^\w\-]+/g,'')// Remove all non-word chars.replace(/\-\-+/g,'-')// Replace multiple - with single -.replace(/^-+/,'')// Trim - from start of text.replace(/-+$/,'');// Trim - from end of text}