add created_at and updated_at fields to mongoose schemas-open source projects Automattic/mongoose
rOOb85
This is how I achieved having created and updated.
Inside my schema I added the created and updated like so:
/** * Article Schema */ var ArticleSchema = new Schema({ created: { type: Date, default: Date.now }, updated: { type: Date, default: Date.now }, title: { type: String, default: '', trim: true, required: 'Title cannot be blank' }, content: { type: String, default: '', trim: true }, user: { type: Schema.ObjectId, ref: 'User' } });
Then in my article update method inside the article controller I added:
/** * Update a article */ exports.update = function(req, res) { var article = req.article; article = _.extend(article, req.body); article.set("updated", Date.now()); article.save(function(err) { if (err) { return res.status(400).send({ message: errorHandler.getErrorMessage(err) }); } else { res.json(article); } }); };
The bold sections are the parts of interest.