How do I pull a value from an array embedded within an array of objects in MongoDB?

Viewed 41

I want to remove a value from the teamUsers array, but I'm struggling because the teamUsers array is within an object which is within an array itself.

The data in Mongo is below. I want to pull user "105" from Team Two.

    Client: {
      teams: [
        { teamID: 123,
          teamName: "Team One",
          teamUsers: [
            "101",
            "102",
            "103"
          ]
        },
        { teamID: 456,
          teamName: "Team Two",
          teamUsers: [
            "104",
            "105",
            "106"
          ]
        }
      ]
    }

I'd appreciate any help out there. My current attempt so far, which I think will pull the entire Team Two. I'm stuck as to how to go further deeper in order to only pull a value from an array inside Team Two:

    let removeUserFromOldTeamUsersArray = await Client.findOneAndUpdate(searchQuery,
      {
        $pull: {
          "teams" {"teamName": "Team Two"}
        }
      }
    )
0 Answers
Related