更新对象内的数组

时间:2016-01-10 10:15:42

标签: javascript arrays json node.js mongodb

我有一个对象,我有一个名为:" timeFrame"其中包含更多对象。我试图更新我的MongoDB,以便根据新对象更新timeFrame数组,这意味着新对象的特定索引中的相同值将在旧数组中。

我的对象:

var newMsg = {  
        "screensId":[parseInt(screenId)],
        "name":"msg6",
        "text":["Won the lottery?","Want to invest your money?","Shulberg investment company","Your money is important to us!"],
        "images":["http://thefilipinoexpat.com/wp-content/uploads/2015/11/04_investment.jpg"],
        "template":"/html/templateA.html",
        "msgTime":5000,
        "timeFrame":[
            { "startDate" : [2016,0,1], "endDate" : [2016,11,31], "days" : [0,1,2,3,4,5,6], "startTime" : 6, "endTime" : 23},
            { "startDate" : [2016,0,1], "endDate" : [2016,11,31], "days" : [5], "startTime" : 13, "endTime" : 20}]  }

我的更新代码:

updateMessage = function(msg) {
    db.collection('messages').update({"name": msg.name},{$set:{msgTime: msg.msgTime,template: msg.template,screensId: msg.screensId,text: msg.text,images: msg.images,timeFrame:msg.timeFrame}},{upsert: false}); 
};

我也试过了timeFrame.$,但它不能很好地工作,因为我希望新对象的timeFrame数组中的每个索引都会被复制到旧对象中。

如果它有帮助,我会使用MongoDB 2.6版标准。

更改timeFrame的示例:

 "timeFrame":[
            { "startDate" : [2017,1,1], "endDate" : [2018,11,21], "days" : [0,1,2,3,4,5,6], "startTime" : 5, "endTime" : 23},
            { "startDate" : [2016,0,1], "endDate" : [2016,11,31], "days" : [5], "startTime" : 13, "endTime" : 20}]  }

新的:

"timeFrame":[
            { "startDate" : [2016,0,1], "endDate" : [2016,11,31], "days" : [0,1,2,3,4,5,6], "startTime" : 6, "endTime" : 23},
            { "startDate" : [2019,4,25], "endDate" : [2019,11,28], "days" : [5], "startTime" : 8, "endTime" : 20}]  }

0 个答案:

没有答案