I know MongoDB doesn\'t support transactions as relational databases do, but I still wonder how to achieve atomicity for several operations. Hunting around the web, I see people
There have been a few attempts to implement solutions. None of them are particularly active at the time of writing. But maybe they work just fine!
https://github.com/anand-io/mongoose-transaction Lacks documentation, 1 year idle.
https://github.com/niahmiah/mongoose-transact Documented, 2 years idle.
https://github.com/rain1017/memdb Offers transaction support but actually does a lot more than just that, 6 months idle.
niahmiah's README is worth looking at. It notes some disadvantages of using transactions, namely:
niahmiah also suggests that there may be alternatives to using transactions.
Just a tip: If you don't like the idea of locking up your frequently used collections, you could consider separating the sensitive (transactional) data from the less sensitive data.
For example, if you are currently holding credits
in the User collection, then you would be better off separating it into two collections: User and UserWallet, and moving the credits
field into the wallets. Then you can freely update User documents without fear of interfering with transactions on the UserWallet collections.