2014-02-08 19 views
0

圖片你有這樣的模式:如何創建Mongoose模型的所有空值樣本?

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 
     } 
    ] 
} 

回答

0

只需添加到您的模型定義「默認:null」:

var schema = new Schema({ 
    name: {type: String, default: null}, 
    etc: {type: whatever, default: null} 
}); 

現在,如果你沒有明確規定領域中,保存「空」的值。