嵌套对象的FindOneAndUpdate

时间:2019-08-26 09:18:12

标签: json mongoose nested graphql

我正在尝试更新JSON的嵌套对象。 我可以编辑该字段,但无法将位置运算符集成为变量。

这是我的JSON的样子:

{
  "id": "3",
  "name": "Deployment",
  "frequency": "15",
  "lastExecuted": "05.05.2018",
  "maintenanceSteps": {
    "maintenanceStep": [
    {
      "id": "1",
      "shortDescription": "ShortDescription",
      "description": "TestDescription",
      "video": "6.1.mp4",
      "image": "LinkToImage",
      "status": "open",
      "duration": "25"
    },
    {
      "id": "2",
      "shortDescription": "ShortDescription",
      "description": "TestDescription",
      "video": "6.1.mp4",
      "image": "LinkToImage",
      "status": "open",
      "duration": "15"
    }
    ]
  }
}

这就是我要设置状态的方式:

resolve: async function (root, param) {
        let setMaintenanceStepStatus = {};
        if (param.status) {
          if(setMaintenanceStepStatus["maintenanceSteps"])
          {
            console.log("true");
            setMaintenanceStepStatus.maintenanceSteps.maintenanceStep[param.id-1].status = param.status;
          }
        console.log("STEP: " + setMaintenanceStepStatus);
        }
        const uMaintenanceStep = await MaintenancePlanModel.findOneAndUpdate({name: param.name},
          { $set:
             {
               "maintenanceSteps.maintenanceStep.1.status": param.status,
              }
          }, {
          new: true
        });
        console.log("MSTEP: " + uMaintenanceStep.maintenanceSteps.maintenanceStep[param.id-1])
        //uMaintenanceStep.maintenanceSteps.maintenanceStep[param.id-1].status = param.status;
        if (!uMaintenanceStep) {
          throw new Error('Error')
        }
        return uMaintenanceStep
      }

问题在于此行中的位置运算符:

"maintenanceSteps.maintenanceStep.1.status": param.status

如何在其中获取我的param.id?

使用

"maintenanceSteps.maintenanceStep." + param.id + ".status": param.status

不幸的是无法正常工作

另一种想法是更改find​​OneAndUpdate,这样我就可以直接访问要更新的维护步骤。

我尝试过:

const uMaintenanceStep = await MaintenancePlanModel.findOneAndUpdate({name: param.name, "maintenanceSteps.maintenanceStep.id": param.id},

但我收到此错误消息:“无法读取null的属性'maintenanceSteps'。”

我的另一个想法是使用findById并通过其objectID搜索maintenanceStep,因此我不必访问任何嵌套的对象。但是在这里,我在模型\“ maintenanceStep \”的路径\“ _ id \”处收到了一个Cast错误“无法成功将值\“ {id:'5d63f6e37276ee1b4cdb99c5'}

1 个答案:

答案 0 :(得分:0)

该问题的答案是,如果要传递串联的字符串,则需要将其作为数组传递:

解决方案是:

["maintenanceSteps.maintenanceStep." + [param.id-1] + ".status"]

这是完整的代码:

 resolve: async function (root, param) {
        let setMaintenanceStepStatus = {};
        let one = "maintenanceSteps.maintenanceStep." + [param.id-1] + ".status"
        if (param.status) {
          if(setMaintenanceStepStatus["maintenanceSteps"])
          {
            console.log("true");
            setMaintenanceStepStatus.maintenanceSteps.maintenanceStep[param.id-1].status = param.status;
          }
        console.log("STEP: " + setMaintenanceStepStatus);
        }
        const uMaintenanceStep = await MaintenancePlanModel.findOneAndUpdate({name: param.name, "maintenanceSteps.maintenanceStep.id": param.id},
          { $set:
             {
                [one]: param.status,
              }
          }, {
          new: true
        });
        console.log("MSTEP: " + uMaintenanceStep.maintenanceSteps.maintenanceStep[param.id-1])
        //uMaintenanceStep.maintenanceSteps.maintenanceStep[param.id-1].status = param.status;
        if (!uMaintenanceStep) {
          throw new Error('Error')
        }
        return uMaintenanceStep
      }
    },