0

please help me with a mongoose code to push a string in an array of a particular sub-document after finding it.

  1. How to find and retrieve a sub-document
  2. Update an array in it
  3. Save it
Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
Sankar Kumar
  • 1
  • 1
  • 2

2 Answers2

0

At first you retrieve the document then you can push like(doc.push(data)) then doc.save()

Shuvro
  • 222
  • 3
  • 7
0

here members is subdocument in club take a look at this. this will definetly solve the problem

router.post("/update", verify, async (req, res) => {
      Club.findOneAndUpdate(
        {
          _id: req.club._id,
          "Members.name": req.club.Mem_Name,
        },
        {
          $set: {
            "Members.$.email": req.body.email,
            "Members.$.phonenumber": req.body.phonenumber,
            "Members.$.graduationyear": req.body.graduationyear,
            "Members.$.reg": req.body.reg,
          },
        },
        { multi: true },
        (err, doc) => {
          if (err) console.log(err);
          else res.json(doc);
        }
      );
    });
Sankar Kumar
  • 1
  • 1
  • 2