如何创建Mongoose Model样本的所有空值?

时间:2014-02-08 21:11:03

标签: javascript node.js mongodb mongoose odm

您拥有此架构的图片:

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

1 个答案:

答案 0 :(得分:0)

只需添加到您的模型定义“default:null”:

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

现在,如果您没有明确地为字段提供值,则将其保存为“null”。