Skip to content
Advertisement

Mongoose date field – Set default to date.now + N days

In a mongoose schema such as:

JavaScript

On the line for the “end” field the default date should set to +7 days. I can add presave hook and set it there, but wondering if theres a way to do this inline in the default field.

Advertisement

Answer

You can add 7 days converted to milliseconds to current date like this

JavaScript

or even like this

JavaScript

UPDATE

Please check the @laggingreflex comment below. You need to set function as default value:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement