簡體   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