title: Enforcing the default value in Mongoose schema
body: <p>I have been trying to find a way to enforce the default value of a schema so that the default value is being used upon insert regardless of any input parameter. In other words a property of a schema should always have the default value and if any other parameter is being passed on insert/write that passed parameter would be ignored.</p>
  
  <p>As an example, please see my dummy schema below. The property I want to enforce is <code>MySchema.created</code>, which is supposed to store the timestamp of the moment document gets created.</p>
  
  <pre><code>var mongoose = require("mongoose");
  
  var MySchema = new mongoose.Schema({
      sendStatus:     {
                          type: String,
                          enum: ["notsent", "sent", "failed"],
                          default: "notsent"
                      },
      created:        {
                          type: Date,
                          default: Date.now
                      },
      creator:        {
                          type: mongoose.Schema.Types.ObjectId,
                          ref: "User",
                          required: true
                      }
  });
  </code></pre>
  
  <p>I did not find an answer in Mongoose documentation (<a href="http://mongoosejs.com/docs/defaults.html" rel="nofollow noreferrer">http://mongoosejs.com/docs/defaults.html</a>) even though it do mention <code>setDefaultsOnInsert</code>, which sounds somewhat close. Altough I would like to do all the schema validation and enforcements on schema level to avoid development mistakes and copy-paste code. Also I don't see how I could use <code>setDefaultsOnInsert</code> for this timestamp, because I want to keep it also constant and not update it upon document update.</p>
  
  <p>Is this possible to achieve? Having a reliable creation date on a document must be a very common use case, but somehow I fail to find a satisfying answer in the internet. Thanks!</p>