title: Mongoose - how to update a child object with information from the parent automatically
body: <p>There is a specific reason to have the information in the following way so I can't change how the parent and child object relate:</p>
  
  <p>I have a parent object that contains information about a multi-day fair. There is a child object that references information for each day of the fair, each of these days is referred to as an event. I need to have the event child objects automatically generated at the time the fair object is generated. Then I need to separately generate an array of ObjectID's from the generated child objects. In addition I need to be able to generate arbitrary event objects at a later time that are not based on the fair object. That's why I have the ObjectID's array so I know what was automatically generated and what was user generated.</p>
  
  <p>This is my schema (just the useful parts):</p>
  
  <pre><code>/**
  * This is the main object, the user only supplies information for this object
  * name, start and end are all provided by the user, num_days is automatically calculated
  */
  
  var FairSchema = new Schema({
  name: {
      type: String,
      default: '',
      required: 'Please fill Fair name',
      trim: true
  },
  start_date: {
      type: Date,
      required: 'Please provide a start date'
  },
  end_date: {
      type: Date,
      required: 'Please provide an end date'
  },
  num_days: {
      type: Number,
      required: true,
      default: 1
  },
  events: EventsSchema
  });
  
  /**
  * This is the child object it should pull all of it's information from the parent at the time the parent is created if
  * the parent object has more than one day an event child object is created for each day.
  *
  * name: The same as the parent object, unless multiday then it's the name plus 'Day #'
  * start_date: The same as the parent object unless multiday then it's the parent object time + the child object day
  * end_date: the same as the parent object unless multiday then it's the parent object time + the child object day
  *
  */
  
  var EventSchema = new Schema({
  name: {
      type: String,
      required: 'Please provide a name for the event',
      trim: true
  },
  start_date: Date,
  end_date: Date,
  });
  </code></pre>
  
  <p>I've tried something like this in my controller and then called it before the fair object is saved:</p>
  
  <pre><code>initializeFairEvents = function (fair) {
  var fairLengthDays = fair.num_days;
  var eventStart = Date.parse(fair.start_date);
  var eventEnd = Date.parse(fair.end_date);
  var one_day = (24 * 60 * 60 * 1000);
  var events = fair.events;
  var dailyEventLength = eventEnd - eventStart - one_day*(fairLengthDays-1);
  
  if (events !== null &amp;&amp; events.length &gt; 0){}
  else {
      for (var i = 0; i &lt; fairLengthDays; i++) {
          var name, start_date, end_date, preserve;
          start_date = (i * one_day) + eventStart;
          end_date = (i * one_day) + eventStart + dailyEventLength;
          preserve = true;
          if (fairLengthDays === 1) {
              name = fair.name;
          } else {
              name = fair.name + ' - day ' + (i + 1).toString();
          }
          fair.events.push({
              name: name,
              start_date: start_date,
              end_date: end_date,
              preserve: preserve
          });
      }
  }
  </code></pre>
  
  <p>};</p>
  
  <p>But then I don't have ObjectID's because nothing has gone to the database to have the ID's generated.</p>
  
  <p>I feel like I'm caught in a loop. Any tips or thoughts would be appreciated.</p>