Closed justinpage closed 2 years ago
can reproduce locally, save
is consistent order, where as findOneAndUpdate
($push
) is in reversed order
Reproduction Repository / Branch: https://github.com/typegoose/typegoose-testing/tree/mongooseGh12475
mongoose debug log:
Prettier log:
{
$set: { updatedAt: new Date("Tue, 27 Sep 2022 11:06:34 GMT"), uuid: "upsertOp" },
$setOnInsert: { __v: 0, createdAt: new Date("Tue, 27 Sep 2022 11:06:34 GMT") },
$push: {
addresses: {
location: "earth",
_id: new ObjectId("6332d93a9999fe1024c129fd"),
updatedAt: new Date("Tue, 27 Sep 2022 11:06:34 GMT"),
createdAt: new Date("Tue, 27 Sep 2022 11:06:34 GMT"),
},
},
}
{
upsert: true,
runValidators: true,
remove: false,
projection: {},
returnDocument: "after",
returnOriginal: false,
}
findOneAndUpdate
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: