title: Mongoose / MongoDB - Simple example of appending to a document object array, with a pre-defined schema
body: <p>For the sakes of simplicity, assuming these are my collection schemas:</p>
  
  <pre><code>var MessageDeliverySchema = new Schema({
      from   : { type : String },
      to     : { type : String },
      status : { type : String }
  });
  
  var Messages = mongoose.model('messages',
  new Schema({
      id              : ObjectId,
      user            : { type:String },
      'sent-messages' : [MessageDeliverySchema]
  }));
  </code></pre>
  
  <p>So each document in the collection <em>Messages</em> may have 4/5 <em>sent-messages</em> defined by the MessageDeliverySchema.</p>
  
  <p>What I want to do is have an array of <em>sent-messages</em>, so each time a delivery receipt comes in I just update <em>Messages</em> and append another sent message.</p>
  
  <p>What I've tried:</p>
  
  <pre><code>var delivered = {
  from: 'foo',
  to: 'bar',
  status: 'delivered'
  };
  
  Message.update({_id: '5064aae4154cb34d14000001' },
          {$pushAll: { 'sent-messages' : delivered }} , {upsert:true}, function(err, data) { 
  
  });
  
  Message.update({_id: '5064aae4154cb34d14000001' },
           {$push: { 'sent-messages' : delivered }},{upsert:true}, function(err, data) { 
  });
  </code></pre>
  
  <p>$pushAll, and $push, doesn't append a new object to <em>sent-messages</em>, instead it overwrites an existing message.</p>
  
  <p>What I want to see is an array of sent messages e.g:</p>
  
  <pre><code>{
      'sent-messages': [
          {
              from: 'foo',
              to: 'bar',
              status: 'delivered'
          },
          {
              from: 'pippo',
              to: 'pippo',
              status: 'delivered'
          },
          {
              from: 'paul',
              to: 'smith',
              status: 'undelivered'
          }
      ]
  }
  </code></pre>