findOneAndUpdate
creates subdocuments with timestamps in reverse order
#12475
Labels
developer-experience
This issue improves error messages, debugging, or reporting
enhancement
This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone
Prerequisites
Mongoose version
6.6.2
Node.js version
18.9.0
MongoDB server version
5.0.5
Description
When using Model.findOneAndUpdate, with a subdocument schema, that has timestamps enabled, the
createdAt
andupdatedAt
properties are inserted in reverse order.In other words,
updatedAt
precedescreatedAt
when automatically inserted in the subdocument.Steps to Reproduce
You can reproduce this behavior by running the following code.
In the example above, we create a new document using the traditional save method. Then, we create another document using the findOneAndUpdate method.
Looking at the database, we can see that the parent document has the timestamps in the expected order:
createdAt
and thenupdatedAt
. However, when looking at the subdocument, created by the findOneAndUpdate method, we will see the timestamps in a reverse order:updatedAt
and thencreatedAt
:Expected Behavior
When using Model.findOneAndUpdate, with a subdocument schema, that has timestamps enabled, the
createdAt
andupdatedAt
properties are inserted in the expected order:The text was updated successfully, but these errors were encountered: