title: Mongoose document schema and validation
body: <p>I Have a schema like so:</p>
  
  <pre><code>class Schemas
  
    constructor: -&gt;
      @mongoose = require 'mongoose'
      @schema = @mongoose.Schema
  
      @EmployeeSchema = new @schema
        'firstname': { type: String, required: true }, 
        'lastname': { type: String, required: true }, 
        'email': { type: String, required: true, index: { unique: true }, validate: /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/ },
        'departmentId': { type: @schema.ObjectId, required: true }
        'enddate': String,
        'active': { type: Boolean, default: true } 
  
      @EmployeeSchemaModel = @mongoose.model 'employees', @EmployeeSchema
  
      @DepartmentSchema = new @schema
        'name': { type: String, required: true, index: { unique: true } }
        'employees' : [ @EmployeeSchema ]
  
      @DepartmentSchemaModel = @mongoose.model 'departments', @DepartmentSchema
  </code></pre>
  
  <p>So that my <code>employees</code> live in an array of <code>employee</code> documents inside a <code>department</code></p>
  
  <p>I have several <code>department</code> documents that have a number of <code>employee</code> documents stored in the <code>employees</code> array. </p>
  
  <p>I then added a new <code>department</code> but it contained no <code>employees</code>. If I then attempt to add another <code>department</code> without <code>employees</code>, Mongoose produces a <code>Duplicate key error</code> for the <code>employee.email</code> field which is a required field. The <code>employee.email</code> field is required and unique, and it needs to be. </p>
  
  <p>Is there anyway round this?</p>