let's say I have a field that contains a default value, I want to use this default value only when I save() the data, but I don't want to see the default value if I fetched data that doesn't include the desired field
//ignore any syntax or any other errors
let dataSchema = new mongoose.Schema({
createdAt: {
type: Date,
default: Date.now
},
....
})
let dataSchemaWithoutDefault = new mongoose.Schema({
createdAt: Date
....
})
let dataModelWithoutDefault = mongoose.model("data", dataSchemaWithoutDefault)
let record = dataModelWithoutDefault.save() //createdAt doesn't present
//let's fetch data but with the default value enabled
let dataModel = mongoose.model("data", dataSchema)
dataModel.find().then(data => console.log(data))
//this with console data with default values, but I need a typical copy from the real collection
//[{createaAt:2018-11-12T06:54:50.119Z},...]