【问题标题】:How to create sample of Mongoose Model all null values?如何创建猫鼬模型的所有空值样本?
【发布时间】:2014-03-06 06:56:15
【问题描述】:

你有这个架构的图片:

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

【问题讨论】:

    标签: javascript node.js mongodb mongoose odm


    【解决方案1】:

    只需在模型定义中添加“default: null”:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 2016-03-09
      • 2012-06-04
      • 2012-04-22
      • 2014-01-09
      • 2015-03-25
      • 2019-11-11
      相关资源
      最近更新 更多