0

I have collection with name Participates in that field ORGANIZATIONS which is array contain object data look like below enter image description here

"ORGANIZATIONS": [{
        "MAINID": "MRINAP4210011",
        "STATUS": "Pending"
    }, {
        "MAINID": "MRINAP4210012",
        "STATUS": "Pending"
    }]

I need to update the "STATUS" according "MAINID" let say MAINID is provide as "MAINID": "MRINAP4210011" so its "STATUS" change to "approved".

   "ORGANIZATIONS": [{
            "MAINID": "MRINAP4210011",
            "STATUS": "approved"
        }, {
            "MAINID": "MRINAP4210012",
            "STATUS": "Pending"
        }]

I am trying below way and stuck

                       let newvaluesorg = {};

                            if (Status == 'Approved') {
                                console.log('Username', VZID);

                                newvaluesorg = {
                                    $set: {
                                        MAINID: "MRINAP4210011",
                                        STATUS: "approved"
                                    }
                                };
                                console.log("newvaluesorg newvalue", newvaluesorg);

                                let query1 = {
                                    "MAINID": mainID
                                };
                                client.db(config.Database.DFARM.dbName).collection("Participates").updateOne(query1, {
                                    $set: newvaluesorg
                                }, function (err, result) {
                                    if (err) {
                                        // console.log(err);

                                        let insertError = new Error(500, "Error updating produce to DFARM", err);
                                        let errors = [];
                                        errors.push(insertError);
                                        output.errors = errors;
                                        res.status(500).json(output);

                                    } else {
                                        // console.log(result);
                                        console.log('res', responses)
                                        output.message = "User updated successfully.";
                                        res.status(200).json(output);
                                    }
                                });
  

Please suggest how to update it.

Thanks

1 Answers1

0

PS: change the "if condition" if you want to move from pending state to approved.

Try this:

               let newvaluesorg = {};

                    if (Status == 'Pending') {
                        console.log('Username', VZID);

                        newvaluesorg = {
                                STATUS: "approved"
                        };
                        console.log("newvaluesorg newvalue", newvaluesorg);

                        let query1 = {
                            "MAINID": mainID
                        };
                        client.db(config.Database.DFARM.dbName).collection("Participates").updateOne(query1, {
                            $set: newvaluesorg
                        }, function (err, result) {
                            if (err) {
                                // console.log(err);

                                let insertError = new Error(500, "Error updating produce to DFARM", err);
                                let errors = [];
                                errors.push(insertError);
                                output.errors = errors;
                                res.status(500).json(output);

                            } else {
                                // console.log(result);
                                console.log('res', responses)
                                output.message = "User updated successfully.";
                                res.status(200).json(output);
                            }
                        });
Selmi Karim
  • 2,135
  • 14
  • 23