How to default an embedded document in null using mongoose

Suppose I have a schema with an embedded document called settings, which contains several values. I am trying to make mongoose the default inline document in null

For example, how do I default fields to null for basic types such as 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 do I default property properties in userSchema to zero when creating a new document?

+5
source share
1 answer
 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" }}) 
+4
source

Source: https://habr.com/ru/post/1210071/


All Articles