Updating nested array of objects within am array of objects

Hi. I have the following 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 would like to push data to a repo array for a specific department, say “IT” with id “1001-002”. I am struggling to find a solution for this issue which seems to be very straightforward.

My issue is very similar to this issue Pushing elements to nested array - Working with Data - MongoDB Developer Community Forums. However our data structure is slightly different, therefore the solution does not really work for my application.

Your assistance will be greatly appreciated.

You should be able to use arrayfilters for this

Thank you John,

You are a lifesaver. The manual did help. Again thank you.

Below is the code sample for those with a similar problem to mine.


      const orgUnitId = req.body.ouId;
      const deptId = req.body.deptId;
      const name = req.body.name;
      const url = req.body.url;
      const username = req.body.username;
      const password = req.body.password;

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

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.