Lets say I have a schema with an embedded document called preferences that contains multiple values. I'm trying to have mongoose default the embedded document to null
For example here's how I've defaulted fields to null for basic types like Strings and Dates in the past.
var userSchema = new Schema({
name: {type: String, required: true},
preferences: {type: preferences, default: null}
});
var preferences: {
preference1: String,
preference2: String
}
How should I default the property preferences in the userSchema to be null on a new document's creation?
var userSchema = new mongoose.Schema({
name: {type: String, required: true},
preferences: {type : { preference1 : String, preference2 : String}, default : null}
});
Tested with:
User.create({name : "Aladin"})
User.create({name : "Aladin", preferences : {preference1 : "Wunderlampen", preference2 : "Teppich" }})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With