I want to have default values for certain fields in my mongoose models. The trick is I don’t want to store these values in the database, but what to add them when the model is initialized.
Please help.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can define an
'init'middleware function for the schema which runs when a model instance is loaded from the database. That should let you manipulate the instance to add your defaults as needed.Also see this related question for more details as the docs are pretty spare on this.