Skip to content

Commit fc1da0b

Browse files
docs(timestamps): elaborate on how timestamps works under the hood
1 parent e6e7af6 commit fc1da0b

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

docs/guide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,11 @@ to your schema. The type assigned is [Date](./api.html#schema-date-js).
11051105
By default, the names of the fields are `createdAt` and `updatedAt`. Customize
11061106
the field names by setting `timestamps.createdAt` and `timestamps.updatedAt`.
11071107

1108+
The way `timestamps` works under the hood is:
1109+
* If you create a new document, mongoose simply sets `createdAt`, and `updatedAt` to the time of creation.
1110+
* If you update a document, mongoose will add `updatedAt` to the `$set` object.
1111+
* If you set `upsert: true` on an update operation, mongoose will use [`$setOnInsert`](https://docs.mongodb.com/manual/reference/operator/update/setOnInsert/) operator to add `createdAt` to the document in case the `upsert` operation resulted into a new inserted document.
1112+
11081113
```javascript
11091114
const thingSchema = new Schema({..}, { timestamps: { createdAt: 'created_at' } });
11101115
const Thing = mongoose.model('Thing', thingSchema);

0 commit comments

Comments
 (0)