6 var mongoose = require('mongoose')
7 , Schema = mongoose.Schema;
13 var BlogPost = new Schema({
17 , comments : [Comments]
20 // recursive embedded-document schema
22 var Comments = new Schema();
25 title : { type: String, index: true }
28 , comments : [Comments]
32 * Accessing a specific schema type by key
40 return v == 'now' ? new Date() : v;
47 BlogPost.pre('save', function(next, done){
48 emailAuthor(done); // some async function
56 function slugGenerator (options){
57 options = options || {};
58 var key = options.key || 'title';
60 return function slugGenerator(schema){
61 schema.path(key).set(function(v){
62 this.slug = v.toLowerCase().replace(/[^a-z0-9]/g, '').replace(/-+/g, '');
68 BlogPost.plugin(slugGenerator());
74 mongoose.model('BlogPost', BlogPost);