Mongoose / MongoDB - Simple example of appending to a document object array, with a pre-defined schema Mongoose / MongoDB - Simple example of appending to a document object array, with a pre-defined schema mongoose mongoose

Mongoose / MongoDB - Simple example of appending to a document object array, with a pre-defined schema


The error I was getting was: 'Cannot apply $push/$pushAll modifier to non-array'.

I added an object to the document like this:

    Message.update({_id: '5064aae4154cb34d14000001' },           { 'sent-messages' : delivered }, function(err, data) {     });

And then tried to do a $push, which gave me the above error.

So I removed the document and used $push which had the desired effect.

For reference this is the correct way to append to an existing document, and the answer to my question:

Message.update({_id: '5064aae4154cb34d14000001' },         {$push: { 'sent-messages' : delivered }},{upsert:true}, function(err, data) { });