Skip to content

Commit 72eb91f

Browse files
committed
docs; Date method caveats
closes Automattic#1598
1 parent aaf0826 commit 72eb91f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

docs/schematypes.jade

+14
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ block content
7979
m.save(callback);
8080

8181
h3 Usage notes:
82+
h4#Dates Dates
83+
:markdown
84+
[Built-in `Date` methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) are [__not__ hooked into](https://github.com/LearnBoost/mongoose/issues/1598) the mongoose change tracking logic which in English means that if you use a `Date` in your document and modify it with a method like `setMonth()`, mongoose will be unaware of this change and `doc.save()` will not persist this modification. If you must modify `Date` types using built-in methods, tell mongoose about the change with `doc.markModified('pathToYourDate')` before saving.
85+
86+
:js
87+
var Assignment = mongoose.model('Assignment', { dueDate: Date });
88+
Assignment.findOne(function (err, doc) {
89+
doc.dueDate.setMonth(3);
90+
doc.save(callback) // THIS DOES NOT SAVE YOUR CHANGE
91+
92+
doc.markModified('dueDate');
93+
doc.save(callback) // works
94+
})
95+
8296
h4#mixed Mixed
8397
p An "anything goes" SchemaType, its flexibility comes at a trade-off of it being harder to maintain. Mixed is available either through Schema.Types.Mixed or by passing an empty object literal. The following are equivalent:
8498
:js

0 commit comments

Comments
 (0)