How can i set default values for subdocument in mongoose?

1,205 views
Skip to first unread message

Bikram Basnet

unread,
Dec 17, 2013, 6:13:26 PM12/17/13
to mongoo...@googlegroups.com
I wana set default values for mongoose subdocument..for example lets say i have user schema

var listschema=new Schema({
name:String
})

var user=new Schema({
name:String,
email:String,
password:String,
gender:{ type:String, default:'none' },
birthdate:Date.
lists:[listschema]
})
i wana set default value for lists like  [ { _id: 52a00114d9aaa04614000047, name: 'Anniversary' },
       { _id: 52a00114d9aaa04614000048, name: 'Baby Registry' },
       { _id: 52a00114d9aaa04614000049, name: 'Birthday' }]
this value should be available for  every new users.whenver i update the lists with new value.this updated value should also reflects all the users on my application..Please help me .i wll be very kind enough

Paul Verhoeven

unread,
Dec 19, 2013, 8:38:02 AM12/19/13
to mongoo...@googlegroups.com
You can set defaults in your subdocument schema (or any schema for that matter) like i did here in your listschema:

var listschema = new Schema({
    name
: {
        type
: String,
       
default: 'value'
   
}
});


var user = new Schema({

    name
: String,
    email
: String,
    password
: String,
    gender
:{
        type
:String,
       
default:'none'
   
},
    birthdate
:Date.
    lists
:[listschema]
});



Op woensdag 18 december 2013 00:13:26 UTC+1 schreef Bikram Basnet:

Bikram Basnet

unread,
Dec 19, 2013, 10:03:55 PM12/19/13
to mongoo...@googlegroups.com
Very much thankful for your reply..i did like your said..but it doesnt get the value from the mongodb collection(which exactly maps this schema).i want to get the user all information with given list stored in mongodb collection.please help
Reply all
Reply to author
Forward
0 new messages