如何在 mongoose 中使用 children 为 属性 分配 "null" 或 "undefined"

How to assign "null" or "undefined" for a property with children , in mongoose

如果我还没有在 mongoose 中初始化它,我想制作 "address" 属性 "null" 或 "undefined"。我试图将它初始化为空值,但没有成功。

var userSchema = new Schema({
    name: { type: String, default: null },
    address: {
      number: { type: String, default: null },
      route: { type: String, default: null },
      areaInner: { type: String, default: null },
      areaOuter: { type: String, default: null },
      country: { type: String, default: null }
    }
},{timestamps:true});

var User = mongoose.model('users', userSchema);

var newUser = User({
        name: name,
        address:null
});

newUser.save();

您必须为 "address" 定义其他架构:

var addressSchema = new Schema({
  number: {type: String, default: null},
  ...
}); 

var userSchema = new Schema({
  name: {type: String, default: null},
  address: {type: addressSchema, default: null}
});