Using four repeating command blocks, I was able to lock mobs inside a rectangular area while I myself was able to pass & hit through it.
These command blocks use the /execute as command, then specifying an area, in this case a wall, of coordinates using @e[x=?,y=?,z=?,dx=9,dy=10,dz=0] where x, y, and z select a block and dx, dy, and dz specify the dimensions of the area extending from the block.
Then I used the data merge command, affecting any mobs within those areas with a motion tag that would apply a velocity of 0.1, sending them away from the wall.
Here's a final version of the command:
/execute as @e[x=61,y=67,z=-128,dx=0,dy=10,dz=-9] run data merge entity @s {Motion:[-0.1,0.0,0.0]}
The values in the motion tag correspond to movements of x, y, and z.
Feel free to leave any questions in the comments, and be sure to let me know if you would like a tutorial video for some of my other creations!
![](https://i.ytimg.com/vi/K4I0zcs6DsM/maxresdefault.jpg)