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

霸气de小男生 提交于 2019-12-03 10:03:39

问题


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'
        }
    ]
}

回答1:


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) { 
});


来源:https://stackoverflow.com/questions/12630788/mongoose-mongodb-simple-example-of-appending-to-a-document-object-array-wit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!