How do you store follow/following information in MongoDB and update two documents while applying "all or none" principle. We are developing a small app where users can follow other users - so I am storing two properties "followers and following" in each user document.
When user A follows B then both properties in A & B documents should be updated. How do I make sure if one update fails the process should be rolled back?
P.S. I know a graph database is more suitable for this. But how will you do it if you are stuck with MongoDB?
If both documents share the same relational property then you can use db.collection.update(). docs.mongodb.org/manual/reference/method/db.colle…
But I cannot imagine how such is-friend-of relation can be described to allow a multi-document update.
Denny Trebbin
Lead Fullstack Developer. Experimenting with bleeding-edge tech. Irregularly DJ. Hobby drone pilot. Amateur photographer.
Jan Vladimir Mostert
Idea Incubator
MongoDB is not ACID compliant, hence you won't be able to do two commits atomically. The closest you'll get is using two-phase commits and rolling your own transactions and roll-back, see the MongoDB docs for more information on how to do this: docs.mongodb.org/manual/tutorial/perform-two-phas…