简体   繁体   中英

Update mongoDB document with foreach of javascript

I have this document/object in my mongoDB database and I will update containerType and scheduleContainerTypeEnums from OVERTIME_POOL to SPARE_TIME . Therefore I tried this:

db.ScheduleRecordMonthCompendiumConfirmation.find().forEach(function (doc) {    
    doc.scheduleRecordDayCompendiums.forEach(function (sch) {
       sch.confirmedScheduleIntervalContainers.forEach(function (c) {
           if (c.containerType === "OVERTIME_POOL") {
               c.containerType = "ADDITIONAL_HOURS";                       
            }
        }
    });
db.ScheduleRecordMonthCompendiumConfirmation.save(doc);
});

But with no success. Does anyone know what I 'm doing wrong?

{
"_id" : ObjectId("57f283d4e4b039723ca44d64"),
"_class" : "mydomain.MyClass",
"scheduleRecordDayCompendiums" : [  
    {
        "_id" : null,
        "institutionUserConnectionId" : "57127240e4b0f77e64ea560e",         
        "confirmedScheduleIntervalContainers" : [
            {
                "_id" : null,
                "containerType" : "OVERTIME_POOL",              
            }           
        ],
        "scheduleContainerTypeEnums" : [
            "OVERTIME_POOL"
        ],
        ...
db.ScheduleRecordMonthCompendiumConfirmation.find().forEach(function (doc) {    
    doc.scheduleRecordDayCompendiums.forEach(function (sch) {
       sch.confirmedScheduleIntervalContainers.forEach(function (c) {
           if (c.containerType === "OVERTIME_POOL") {
               c.containerType = "ADDITIONAL_HOURS";                       
            }
        }
    });

 doc.save(function(error){
  if(error){
    console.log(error)
  }
 });
});

Your javascript is not correctly structured. You are missing closing parenthesis for doc.scheduleRecordDayCompendiums.forEach

db.ScheduleRecordMonthCompendiumConfirmation.find().forEach(function (doc) {    
    doc.scheduleRecordDayCompendiums.forEach(function (sch) {
       sch.confirmedScheduleIntervalContainers.forEach(function (c) {
           if (c.containerType === "OVERTIME_POOL") {
               c.containerType = "ADDITIONAL_HOURS";                       
            }
        })
    });
 db.ScheduleRecordMonthCompendiumConfirmation.save(doc);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM