I have this mongoose model
const reportUsSchema = new mongoose.Schema({
texts: [
{
text: { type: String, default: "" },
date_time: { type: Date, default: new Date() },
},
],
});
I want this:
Whenever a new text is pushed in the texts
field I want to insert the current datetime.
What I am getting (Problem):
I am getting the same date time when I restarted the server last time. For example if I have restarted the server 2 days ago I am getting the 2 days before date, which is wrong.
What I have tried:
- Using moment.js: I have tried multiple combinations of moment.js too. Adding them here:
date_time: { type: Date, default: moment() }
And
date_time: { type: Date, default: moment().format() }
And
date_time: { type: Date, default: moment().utc(true) }
And
date_time: { type: Date, default: moment().utc(true).format() }
- Using the built-in Date()
I am using it currently in the above code but none of above worked for me.
Note: The thing that worked is instead of depending upon this default value from the mongoose model, I have passed the current datetime value alongwith the text. And this is working well.
I could not understand the behavior. Help me understand where I was wrong.
Mongoose version: 5.10.7
2
Answers
The default: new Date() option in the schema means that the date_time field will have a default value of the current date and time when the document is created or when the field is not specified.
You can use the timestamps option:
You can also specify the timestamp fileds:
Based on your comment from this answer you need to update your schema to use
Date.now
instead ofnew Date()
like this:This is because when you use
default: new Date()
you are telling the schema to use a default date at the time the schema was defined, not the document. So if you deploy your app to a server and then start up your app the default value for that field in that schema will be the time your app started.However, if you use
Date.now
then mongoose knows to set the default to use the datetime at the time of document creation.In the mongoose docs they do recommend this:
updated: { type: Date, default: Date.now },
in their kitchen sink example. They also list it here in their Default Functions example.