'Creating a new mongoose model date value by adding two existing separate values within the model
I'm currently working on an ExpressJS project that involves mongoose for structuring data, but in particular, I want to use dates as a big part of this project. What I'd like to have in the end is a new mongoose model item created by adding two existing values within the schema together upon creation. So, if a user creates this item (Time), the desired field (end) should be automatically created similar to a createdAt field. So if a user creates a Time item for 3 hours at 9 am, the end value should automatically be created as 12 pm.
Below is an example of what I want to accomplish:
const timeSchema = new mongoose.Schema({
// Start Date/Time filled in a form
date: {type: Date},
// Length of period
hours: {type: Number},
// I want to create an end value by adding these two together
end: {type: Date,
default: mongoose.Schema.date + mongoose.Schema.hours * 60 * 60 * 1000,
ref: "Time",
}
})
module.exports = mongoose.model("Time", timeSchema);
I've attempted a few different methods of combining the elements, but sadly, had no success. All help is much appreciated.
TLDR: Is it possible to create a new date value by adding two existing schema items together?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
