From fe195c37aa462e09ff2b3fafa67a0fff4e92592f Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 6 Feb 2024 16:56:55 -0500 Subject: [PATCH] docs(timestamps): clarify that `replaceOne()` and `findOneAndReplace()` overwrite timestamps Fix #14309 --- docs/timestamps.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/timestamps.md b/docs/timestamps.md index d475fa70af3..17706a6e0f9 100644 --- a/docs/timestamps.md +++ b/docs/timestamps.md @@ -47,6 +47,7 @@ console.log(doc.updatedAt); // 2022-02-26T17:08:13.991Z // Mongoose also blocks changing `createdAt` and sets its own `updatedAt` // on `findOneAndUpdate()`, `updateMany()`, and other query operations +// **except** `replaceOne()` and `findOneAndReplace()`. doc = await User.findOneAndUpdate( { _id: doc._id }, { name: 'test3', createdAt: new Date(0), updatedAt: new Date(0) }, @@ -56,6 +57,35 @@ console.log(doc.createdAt); // 2022-02-26T17:08:13.930Z console.log(doc.updatedAt); // 2022-02-26T17:08:14.008Z ``` +Keep in mind that `replaceOne()` and `findOneAndReplace()` overwrite all non-`_id` properties, **including** immutable properties like `createdAt`. +Calling `replaceOne()` or `findOneAndReplace()` will update the `createdAt` timestamp as shown below. + +```javascript +// `findOneAndReplace()` and `replaceOne()` without timestamps specified in `replacement` +// sets `createdAt` and `updatedAt` to current time. +doc = await User.findOneAndReplace( + { _id: doc._id }, + { name: 'test3' }, + { new: true } +); +console.log(doc.createdAt); // 2022-02-26T17:08:14.008Z +console.log(doc.updatedAt); // 2022-02-26T17:08:14.008Z + +// `findOneAndReplace()` and `replaceOne()` with timestamps specified in `replacement` +// sets `createdAt` and `updatedAt` to the values in `replacement`. +doc = await User.findOneAndReplace( + { _id: doc._id }, + { + name: 'test3', + createdAt: new Date('2022-06-01'), + updatedAt: new Date('2022-06-01') + }, + { new: true } +); +console.log(doc.createdAt); // 2022-06-01T00:00:00.000Z +console.log(doc.updatedAt); // 2022-06-01T00:00:00.000Z +``` + ## Alternate Property Names For the purposes of these docs, we'll always refer to `createdAt` and `updatedAt`.