The catch here is that you're hitting the database as many times as you have matching users, plus the query to find the matching users in the first place. It doesn't hurt much at smaller scales but it'll quickly grind things to a halt once you have more data.
I know Mongo has update with multi or updateMany which could improve matters, although I don't know if Mongoose has different idioms. But honestly, my first recommendation here is to reevaluate Mongo. The portion of your data model shown here is purely relational, and you're having to crudely approximate relational database features to do what you want. Why not just use a relational database?
Thanks for the comment, Dian. I really appreciate it. Yes, normally I would use a relational database for this kind of application due to the relationships between the models. SQL gives you cascades and much more right out of the box.I just wanted to experiment on how cascades could be achieved using mongoose :)
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.