1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| var mongoose = require('mongoose'); var Schema = mongoose.Schema; var blogSchema = new Schema({ title: String, author: String, body: String, comments: [{ body: String, date: Date }], date: { type: Date, default: Date.now }, hidden: Boolean, votes: Number, meta: { createAt: { type: Date, default: Date.now() }, updateAt: { type: Date, default: Date.now() } } }); blogSchema.pre('save', function(next) { if (this.isNew) { this.meta.creatAt = this.meta.updateAt = Date.now(); } else { this.meta.updateAt = Date.now(); } next(); }); module.exports = mongoose.model('Blog', blogSchema);
|