0

I have my schema designed like this

const templateSchema = new Schema({
  Main: {
    text: String,
    textKey: String,
    index: String,
    part: String,
    overallStatus: String,
    subjects: [
      {
        id: String,
        text: String,
        textKey: String,
        index: String,
        type: String,
        comment: String,
        image: String,
        answer: String,
}

and I have to update subjects text and subject id and I am doing it like this

router.post("/edit", (req, res, next) => {
    Template.findOneAndUpdate({
        id: req.body.id,
        text: req.body.text
    }).then(updatedTemp => {
        console.log(updatedTemp);
        if (updatedTemp) {
            res.status(200).json({
                message: "Template updated.."
            });
        } else {
            res.status(404).json({
                message: "Checklist not found"
            });
        }
    });
});

it returns template updated and status 200 but it doesn't update the new values. How can i access subject ID and subject text in this schema

1 Answer 1

0

so, According to the provided schema, you should find inside the array, and update there, you could do something like this:

router.post("/edit", (req, res, next) => {
  Template.update(
    {
      "Main.subjects.id": req.body.oldId,
      "Main.subjects.text": req.body.oldText,
      //you can pass some more conditions here
    },
    {
      $set: {
        "Main.subjects.$.id": req.body.id,
        "Main.subjects.$.text": req.body.text
      }
    }
  )
    .then(updated => {
      if (updated.nModified) {
        res.status(200).json({
          message: "Template updated.."
        });
      } else {
        //not updated i guess
      }
    })
    .catch(error => {
      //on error
    });
});

so payload in body you need to pass :

oldId : <which is currently there>,
oldText:  <which is currently there>,
id: <by which we will replace the id field>
text:<by which we will replace the txt>

NOTE: assuming , id or text will be unique among all the docs.

sample data:

{
    "_id" : ObjectId("5be1728339b7984c8cd0e511"),
    "phases" : [ 
        {
            "phase" : "insp-of-install",
            "text" : "Inspection of installations",
            "textKey" : "",
            "index" : "1.0",
            "subjects" : [...]
        },...]
}

we can update text here like this [in the top most level]:

Template.update({
"_id":"5be1728339b7984c8cd0e511",
"phases.phase":"insp-of-install",
"phases.text":"Inspection of installations"
},{
    "$set":{
        "phases.$.text":"Some new text you want to set"
    }
}).exec(...)

but, incase you want to do deep level nested update, you can have a look at this answer : here by @nem035

Sign up to request clarification or add additional context in comments.

11 Comments

{ n: 0, nModified: 0, ok: 1 }
can you show me the out put of Template.find({ "Main.subjects.id": req.body.oldId, "Main.subjects.text": req.body.oldText, }).select({"Main.subjects.$":1}).exec() ?
on console its : { n: 0, nModified: 0, ok: 1 } and on my client side which is Angular 4 : not found please try again
its backend code, can you try the query i have given in the above comment and give the output? will be easy to help then, and it has nothing to do with angular.
thats not working either no output because now I cannot click the button
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.