For the sakes of simplicity, assuming these are my collection schemas:
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]
}));
So each document in the collection Messages may have 4/5 sent-messages defined by the MessageDeliverySchema.
What I want to do is have an array of sent-messages, so each time a delivery receipt comes in I just update Messages and append another sent message.
What I've tried:
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) { 
});
$pushAll, and $push, doesn't append a new object to sent-messages, instead it overwrites an existing message.
What I want to see is an array of sent messages e.g:
{
    'sent-messages': [
        {
            from: 'foo',
            to: 'bar',
            status: 'delivered'
        },
        {
            from: 'pippo',
            to: 'pippo',
            status: 'delivered'
        },
        {
            from: 'paul',
            to: 'smith',
            status: 'undelivered'
        }
    ]
}
                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) { 
});
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With