When are the timestamp added to the schema instance ?

21 views
Skip to first unread message

maxime chaillet

unread,
Jun 24, 2020, 8:48:08 AM6/24/20
to Mongoose Node.JS ODM
Hello,

I would like to know when the timestamps `createdAt` and `updatedAt` are added to the schema instance and what (ie mongoose or mongo) does the job.

From the schema below

````
const Schema = mongoose.Schema(
  {
///whatever
  },
  {
    timestamps: true,
  }
);


module.exports = mongoose.model("MySchema", Schema);

```

then

```
let mySchema = new MySchema({...})
// at this point in time the timestamp values do not exist in mySchema

mySchema().save().then( (data) => {
// at this point in time the timestamps have been created
   } )

```

Are the timestamp created during the save, or in a pre-save hook, or in a post-save-hook ? 
Are the timestamp created by mongoose itself before connecting to the mongo database? or are they created by the mongo database itselve from instructions provided by mongoose.

Thanks for your help

Reply all
Reply to author
Forward
0 new messages