Image 你有这个模式:
var userSchema = new Schema({
name: String,
schools: [{
level: String,
name: String,
timeInterval: {
start: {type:Date,default:Date.now},
end: {type:Date,default:Date.now}
},
location: String
}]
});
有没有办法做一些事情来获取填充不佳的对象。有点像:
var sample = userSchema.getDefaultObject();
console.log(JSON.stringify(sample,null,2));
OUTPUT:
{
name: null,
schools: [
{
level: null,
name: null,
timeInterval: {
start: TIMENOW,
end: TIMENOW
},
location: null
}
]
}
只需将"default: null"添加到模型定义中:
var schema = new Schema({
name: {type: String, default: null},
etc: {type: whatever, default: null}
});
现在,如果您没有明确提供字段的值,它将以"null"保存。