如果我没有在mongoose中初始化它,我想使“地址”属性为“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();
您必须为“地址”定义其他架构:
var addressSchema = new Schema({
number: {type: String, default: null},
...
});
var userSchema = new Schema({
name: {type: String, default: null},
address: {type: addressSchema, default: null}
});