0

I'm using MongoDB with mongoose, and my document is looking like this:

{
 name: "bob"
 employees: [
   {
     ...,
     id: "__sdab234"
   },
   {
     ...,
     id: "__wqerew544"
   }
 ]
 patients: [
      {
       ...,
       employeeSelected: "__sdab234"(id of employee)
      }

I seem to be able to push to patients array, but not add or change any data of an existing object inside the array.

My code:

User.findOne({ _id: id }, function(err, user) {
      if (!user || user === null) res.sendStatus(403);


      const patientFound = user.patients.filter(
        patient => patient.id === idPatientSent
      );
      //check if found
      if (patientFound.length === 0) return res.sendStatus(401);

      patientFound[0].employeeSelected = idEmployeeSent;

      user.save();

      console.log(user.patients)

      //send employee to client
      res.status(200).send({
        message: "Successfully updated selected employee!"
      });
    });

It seems like the changes are made, but does not persist.

  • 2
    Hi John. Multiple similar questions on the same topic have already been answered. You can start from these: https://stackoverflow.com/questions/24054552/mongoose-not-saving-nested-object; https://stackoverflow.com/questions/47123599/mongoose-instance-save-not-working-when-embedded-array-object-changed. If this does not help you, please comment. – Avius Mar 02 '20 at 17:39
  • 1
    Check this. It may be solve the problem https://stackoverflow.com/questions/46190153/update-object-inside-the-array-in-mongodb-using-mongoose – Naynajith Mar 02 '20 at 17:39
  • 1
    I'm not sure who down voted this, but regardless (if it was because other questions exist), the quality of the question is not bad. Additionally, down voting and not leaving feedback doesn't help anyone. Please do not abuse the down vote feature. – The Muffin Man Mar 02 '20 at 18:07
  • 1
    Does this answer your question? [arrayFilters in mongodb](https://stackoverflow.com/questions/51324876/arrayfilters-in-mongodb) – whoami - fakeFaceTrueSoul Mar 02 '20 at 18:32
  • @Avius Thanks a lot, one of the answers(the mark modified method) did the trick for me! – JohnGIlyadov Mar 02 '20 at 23:31
  • 2
    @the-muffin-man I downvoted the question. It is indeed quite well-formed, but that's only one criterion. My opinion is that it is not useful since it only adds to the already significant noise of nearly identical questions whilst also displaying a lack of effort to solve the problem unaided, as it is surely not that difficult. I admit, I could've expressed the reasoning more clearly, sorry about that. – Avius Mar 03 '20 at 09:57
  • @Avius thanks for replying with your reasoning! – The Muffin Man Mar 03 '20 at 17:59
  • @Avius I'd recommend not downvoting on the basis of assumption, I did look for an answer for this question(for several days) and spend effort trying to solve this problem. My amount of questions here is to prove that I spend several days on each problem I'm stuck on instead of running to post it – JohnGIlyadov Feb 02 '21 at 14:44

0 Answers0