mongoose timestamps createdAt added to existing documents upon doc.save() #11024
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
nodejs version 14.16.0
mongoose version 5.12.5
MongoDB driver (nodejs) 3.6.5
mongoose timestamps configuration:
{timestamps: {createdAt: 'createdAt', updatedAt: 'lut'}}
Steps for reproducing:
After saving, the createdAt field is added to the document with the current date which of course is not the expected behavior since the createdAt field should reflect the actual document creation date (which was created much earlier)
It will not happen when:
Using findAndUpdate (much better the findOne and save - I know)
Using findOne with field projection
As a temporary workaround, I added the following to all schemas using timestamps
createdAt: {type: Date, select: false}
When the above is added, the createdAt field is not added to the existing document upon saving but this is definitely intended as a quick-fix and not a permanent solution since the expected behaviour is that the createdAt field will not be added to an existing document if not specifically requested.
The text was updated successfully, but these errors were encountered: