Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
        }
    ]
}
like image 225
StuR Avatar asked Sep 27 '12 22:09

StuR


1 Answers

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) { 
});
like image 179
StuR Avatar answered Oct 24 '22 22:10

StuR