繁体   English   中英

使用foreach的JavaScript更新mongoDB文档

[英]Update mongoDB document with foreach of javascript

我的mongoDB数据库中有此文档/对象,我会将containerTypescheduleContainerTypeEnumsOVERTIME_POOL更新为SPARE_TIME 因此,我尝试了这个:

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

但是没有成功。 有人知道我在做什么错吗?

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

您的javascript的结构不正确。 您缺少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);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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