0

Image you have this schema:

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

Is there a way to do something to get a poorly populated object. Kinda like:

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
        }
    ]
}
190290000 Ruble Man
  • 2,173
  • 1
  • 20
  • 25

1 Answers1

0

Just add to your model definition "default: null":

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

Now if you do not explicitly provide a value for field it is saved with 'null'.

Eugene Kostrikov
  • 6,799
  • 6
  • 23
  • 25