在nodejs mongodb中的嵌套数组中更新特定的单个子文档

时间:2018-10-23 07:38:32

标签: node.js mongodb

我是nodejs和mongo db的新手。我在mongo中嵌套了子文档类型架构。文档类型为流程=>子流程​​=>任务=>配置。这些是嵌套数组的格式,可以是多个。

{
    "_id" : ObjectId("5bcec0ee711fe511f4848c1d"),
    "process_title" : "customer acquisition",
    "subprocess" : [ 
        {
            "_id" : ObjectId("5bcec0f8711fe511f4848c1e"),
            "subprocess_title" : "application",
            "tasks" : [ 
                {
                    "_id" : ObjectId("5bcec158711fe511f4848c1f"),
                    "task_title" : "pre screening",
                    "task_slug" : "pre-screening",
                    "task_configs" : [ 
                        {
                            "_id" : ObjectId("5bcec4b912582b01b84fe47a"),
                            "next_task" : "thanks"
                        }
                    ]
                }, 
                {
                    "_id" : ObjectId("5bcec190711fe511f4848c20"),
                    "task_title" : "thanks",
                    "task_slug" : "thanks",                       
                    "task_configs" : [ 
                        {
                            "_id" : ObjectId("5bcec469ab23ab1fc0bbb9ed"),
                            "form_field" : "responseMessage",
                            "expression" : "=",
                            "expression_value" : "Approved4",
                            "success_task" : "signup",
                            "success_sub_process_id" : "5bcec0f8711fe511f4848c1e",
                            "fail_task" : "thanks",
                            "fail_sub_process_id" : "5bcec0f8711fe511f4848c1e"
                        }
                    ]
                }, 
                {
                    "_id" : ObjectId("5bcec1c3711fe511f4848c21"),
                    "task_title" : "signup",
                    "task_slug" : "signup",                        

                    "task_configs" : [ 
                        {
                            "_id" : ObjectId("5bcec469ab23ab1fc0bbb9ed"),
                            "form_field" : "responseMessage",
                            "expression" : "=",
                            "expression_value" : "Approved4",
                            "success_task" : "signup",
                            "success_sub_process_id" : "5bcec0f8711fe511f4848c1e",
                            "fail_task" : "thanks",
                            "fail_sub_process_id" : "5bcec0f8711fe511f4848c1e"
                        }
                    ]
                }
            ]
        }
    ],
    "created_at" : ISODate("2018-10-23T06:34:22.676Z"),
    "updated" : ISODate("2018-10-23T06:34:22.676Z"),
    "__v" : 0
}    

现在,我想为特定任务更新task_configs。我正在根据task_slug更新task_config。

1 个答案:

答案 0 :(得分:0)

I found a  way myself to update above query.

Workflow.updateOne({ "_id": new ObjectId(req.body.process_id), "subprocess._id": new ObjectId(req.body.sub_process_id), "subprocess.tasks.task_slug": req.body.task_slug },
    {
      $set: {
        'subprocess.$.tasks.$[j].task_configs': req.body.task_configs
      }
    },
    {
      arrayFilters: [
        {
          "j.task_configs": req.body.task_configs
        }]
    })
    .then(function (resp) {
      console.log(resp)
      res.json({ status: 'success', resp });
    }).catch(function (err) {
      res.status(500).json(err);
    })