Remove nested object from array

I got this user schema

const UserSchema = new Schema({

  email: {

    type: String,

    required: true,

    unique: true,

  },

  groups: [

    {

      groupName: {

        type: String,

        required: true,

      },

      groupMembers: [{ type: Schema.Types.ObjectId, ref: "GroupMember" }],

    },

  ],

});

And I want to delete a group from the ‘groups’ array based on given ‘userId’ and ‘groupId’, my attempt (with express and mongoose):

router.delete(

  "/:userId/:groupId",

  catchAsync(async (req, res) => {

    const { userId, groupId } = req.params;

    const updatedUser = await User.findByIdAndUpdate(

      userId,

      { $pull: { "groups.$._id": groupId } },

      { new: true }

    );

    res.send({ updatedUser });

  })

);

Forgot to add the response of the request: I get an error: “The positional operator did not find the match needed from the query.”