{"id":8121,"date":"2015-11-27T03:33:13","date_gmt":"2015-11-27T03:33:13","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/27\/add-created_at-and-updated_at-fields-to-mongoose-schemas-open-source-projects-automattic-mongoose\/"},"modified":"2022-08-30T15:03:00","modified_gmt":"2022-08-30T15:03:00","slug":"add-created_at-and-updated_at-fields-to-mongoose-schemas-open-source-projects-automattic-mongoose","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/27\/add-created_at-and-updated_at-fields-to-mongoose-schemas-open-source-projects-automattic-mongoose\/","title":{"rendered":"add created_at and updated_at fields to mongoose schemas-open source projects Automattic\/mongoose"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/0a31e244b8fdee38e048d6b6446fc2fc?s=128&amp;d=identicon&amp;r=PG&amp;f=1\" \/> <strong>rOOb85<\/strong><\/p>\n<p>This is how I achieved having created and updated.<\/p>\n<p>Inside my schema I added the created and updated like so:<\/p>\n<pre> \/** * Article Schema *\/ var ArticleSchema = new Schema({ <b>created: { type: Date, default: Date.now }, updated: { type: Date, default: Date.now },<\/b>\n        title: {\n            type: String,\n            default: '',\n            trim: true,\n            required: 'Title cannot be blank'\n        },\n        content: {\n            type: String,\n            default: '',\n            trim: true\n        },\n        user: {\n            type: Schema.ObjectId,\n            ref: 'User'\n        }\n    });\n<\/pre>\n<p>Then in my article update method inside the article controller I added:<\/p>\n<pre>\n\/** * Update a article *\/ exports.update = function(req, res) { var article = req.article; article = _.extend(article, req.body); <b>article.set(\"updated\", Date.now());<\/b>\n\n        article.save(function(err) {\n            if (err) {\n                return res.status(400).send({\n                    message: errorHandler.getErrorMessage(err)\n                });\n            } else {\n                res.json(article);\n            }\n        });\n    };\n<\/pre>\n<p>The bold sections are the parts of interest.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: &#8221;, trim: true, required: &#8216;Title [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8121","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8121"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8121\/revisions"}],"predecessor-version":[{"id":8605,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8121\/revisions\/8605"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}