编辑仅出现一些文档的内部数组

时间:2014-09-18 23:03:45

标签: javascript mongodb

我在mongo中有一个JSON条目:

{
_id: "fe50fdee-4ea3-4824-94af-f369633c0c7a",
_class: "com.tracking.daoservice.model.TrackingData",
modified: ISODate("2014-09-10T23:38:48.407Z"),
eventtype: "William-Test",
eventdata: {
    QueryDate: "01-APR-2014",
    SearchQuery: {
        keyword: "Java",
        location: "Santa Clara, CA",
        Facet: "skill~java~perl|workAuth~USC",
        SearchAgentId: "4299"
    },
    Viewed: [
        {
            ViewedID: "8992ade400a",
            Dockey: "3323aba3233",
            PID: "32399a",
            actionsTaken: "email|direct message|report seeker",
            viewDate: "01-APR-2014",
            MessageSent: "true",
            Message: [
                {
                    MessageID: "123aca323",
                    Delivered: "True",
                    Opened: "True",
                    ClickThroughRate: "NotBad",
                    MessageDate: "02-APR-2014",
                    Response: [
                        {
                            ResponseId: "a323a9da",
                            ResponseDate: "23-APR-2014"
                        }
                    ]
                }
            ]
        }
    ]
},
eventsource: "API-Dev Test - JMachine",
sourceip: "myIp",
entityid: "TmoneyBunnyWunny",
groupid: "Dice",
datecreated: ISODate("2014-09-10T23:38:48.405Z")
}

我有一个更改内部日期属性的脚本,

db.TRACKING_DATA.find().forEach(function(doc) { 
  db.TRACKING_DATA.update({ 
    "_id": doc._id,
    "eventdata.Viewed.viewDate":doc.eventdata.Viewed[0].viewDate
  }, { 
    "$set": { "eventdata.Viewed.$.Message.0.MessageDate": new Date( doc.eventdata.Viewed[0].Message[0].MessageDate) }
  } 
  ) 
});

问题不是所有文档都包含内部对象:

  Message: [
                {
                    MessageID: "123aca323",
                    Delivered: "True",
                    Opened: "True",
                    ClickThroughRate: "NotBad",
                    MessageDate: "02-APR-2014",
                    Response: [
                        {
                            ResponseId: "a323a9da",
                            ResponseDate: "23-APR-2014"
                        }
                    ]
                }
            ]

所以当我运行它时,它会返回:

TypeError: Cannot read property '0' of undefined

我需要一种方法基本上说:

 if(Message exists change these entries within it)

我尝试过多种变体:

db.TRACKING_DATA.find().forEach(function(doc) { 
  db.TRACKING_DATA.update(
  { 
    "_id": doc._id,
    "eventdata.Viewed.viewDate":doc.eventdata.Viewed[0].viewDate, doc.eventdata.Viewed[0].Message[0]: {$exists}
  }, 
  { 
    "$set": {"eventdata.Viewed.$.Message.0.MessageDate": new Date( doc.eventdata.Viewed[0].Message[0].MessageDate) }
  } 
  )
});

但无法找到可行的解决方案。

干杯,

1 个答案:

答案 0 :(得分:1)

添加一些判断会有所帮助。

db.TRACKING_DATA.find().forEach(
    function(doc) {
        var Viewed = doc.eventdata.Viewed;
        if (Viewed instanceof Array && Viewed[0]) {
            var Message = Viewed[0].Message;
            if (Message instanceof Array && Message[0]) {

                db.TRACKING_DATA.update({
                    "_id" : doc._id,
                    "eventdata.Viewed.viewDate" : Viewed[0].viewDate
                }, {
                    "$set" : {
                        "eventdata.Viewed.$.Message.0.MessageDate" : new Date(Message[0].MessageDate)
                    }
                });
            }
        }
    });

我想您希望在MessageDate的每个Message上将字段类型从字符串更改为日期。如果为true,则需要更改这些代码。以上仅根据您的原始代码更改第一个元素。