findOneAndUpdate clearing values in nested array

Hello,

I have the following schema

const mongoose = require("mongoose");

// Create schema
const OrgUnitsShema = mongoose.Schema({
  id: {
    type: String,
    unique: true,
    required: true,
  },
  name: {
    type: String,
    required: true,
  },
  departments: 
    [{
      id: {type: String, required: false, unique: true},
      name: {type: String, required: false},
      repo: [{
        // _id: {type: String, required: false, unique: true},
        name: {type: String, required: false, unique: true},
        url: {type: String, required: false},
        username: {type: String, required: false},
        password: {type: String, required: false},
      }],
    }],
});

// Create Model
const OrgUnitsModel = mongoose.model("organisational_units", OrgUnitsShema);
module.exports = OrgUnitsModel;

With the following sample data

{
    "_id": {
        "$oid": "6504d7f74b847364fc8d192b"
    },
    "id": "1001",
    "name": "News management",
    "departments": [
        {
            "id": "1001-001",
            "name": "Finance",
            "repo": [
                {
                    "name": "Udemy",
                    "url": "https://www.udemy.com/",
                    "username": "cool-tech@udemy.com",
                    "password": "udemy123"
                },
                {
                    "name": "Google",
                    "url": "https://www.google.com/",
                    "username": "user123",
                    "password": "googlepass"
                },
                {
                    "name": "Amazon",
                    "url": "https://www.amazon.com/",
                    "username": "shopper99",
                    "password": "amaz0n!"
                },
                {
                    "name": "Netflix",
                    "url": "https://www.netflix.com/",
                    "username": "bingewatcher",
                    "password": "netflixislife"
                },
                {
                    "name": "Twitter",
                    "url": "https://twitter.com/",
                    "username": "tweetmaster",
                    "password": "tweettweet"
                }
            ],
            "employees": [
                "ObjectId('65057518f0a44c810d387932')"
            ]
        },
        {
            "id": "1001-002",
            "name": "IT",
            "repo": [
                {
                    "name": "Instagram",
                    "url": "https://www.instagram.com/",
                    "username": "instaaddict",
                    "password": "insta1234"
                },
                {
                    "name": "LinkedIn",
                    "url": "https://www.linkedin.com/",
                    "username": "connectme",
                    "password": "linkedinpass"
                },
                {
                    "name": "Facebook",
                    "url": "https://www.facebook.com/",
                    "username": "fbuser",
                    "password": "fb12345"
                },
                {
                    "name": "YouTube",
                    "url": "https://www.youtube.com/",
                    "username": "youtuber",
                    "password": "youtube101"
                }
            ],
            "employees": []
        },
        {
            "id": "1001-003",
            "name": "Writing",
            "repo": [
                {
                    "name": "Spotify",
                    "url": "https://www.spotify.com/",
                    "username": "musiclover",
                    "password": "spotifytunes"
                },
                {
                    "name": "Microsoft",
                    "url": "https://www.microsoft.com/",
                    "username": "msuser",
                    "password": "microsoftpass"
                },
                {
                    "name": "Apple",
                    "url": "https://www.apple.com/",
                    "username": "applefan",
                    "password": "apple123"
                }
            ],
            "employees": []
        }     
        
    ],
    "employees": []
}

I am trying to edit a specific repo entry, say “Instagram” inside department “IT” with id “1001-002” by changing the password for “Instagram” from “insta1234” to insta12345". I used the sample code below but the problem is that it clears out all the other fields of the entry instead of just modifying the one, updated, field within the repo entry.

const result = await OrganisationalUnit.findOneAndUpdate(
        {
          id: orgUnitId,
        },
        {
          $set: {
            "departments.$[dept].repo.$[r]": changes,
          },
        },
        {
          new: true,
          arrayFilters: [
            { "dept.id": { $eq: deptId } },
            { "r.name": { $eq: repoKey } },
          ],
        }
      );

There could be something wrong with my arrayFilters. Your assistance will be highly appreciated.

Possible workaround
I could possibly retrieve all the specific entry details before I “$set” the object. I am sure there is a better way than doing this, hence I logged the ticket.

Additional info 1
The editing is a problem, but adding a new repo entry works fine using the code below.

      const result = await OrganisationalUnit.updateOne(
        {
          id: orgUnitId,
        },
        {
          $push: {
            "departments.$[department].repo": {
              name: name,
              url: url,
              username: username,
              password: password,
            },
          },
        },
        { arrayFilters: [{ "department.id": { $eq: deptId } }] }
      );

Additional info 2
I am updating(editing) another model on one of my projects using the code below and it works. The difference is that the Job model below does not have nested arrays, hence I suspect the problem may be the arrayFilters.

const doc = await Job.findOneAndUpdate(filter, update, {
      new: true,
    });