1 00:00:02,140 --> 00:00:03,870 So what are transactions? 2 00:00:04,090 --> 00:00:09,400 Well let's consider this use case, we have a users and we have a posts collection and we get a couple 3 00:00:09,400 --> 00:00:15,460 of documents in there and let's say each user or most users have a couple of posts. So the posts are 4 00:00:15,460 --> 00:00:19,480 related to users because the user is the person who created the post. 5 00:00:19,750 --> 00:00:27,460 So we have that stored relation, maybe stored with a reference, so with a key stored in a user 6 00:00:27,460 --> 00:00:29,890 or in a post document, doesn't really matter. 7 00:00:29,890 --> 00:00:36,280 Now we delete the user account and therefore we want to delete the documents here together, 8 00:00:36,280 --> 00:00:40,660 so in two collections. Now this can be done without transactions, 9 00:00:40,660 --> 00:00:43,600 we can simply delete a user 10 00:00:44,110 --> 00:00:49,930 and right before we do that, we save the ID of that user, we find out the user id and then we reach out 11 00:00:49,930 --> 00:00:55,680 to the posts collection, find all posts who are linking to that user ID and delete these posts, 12 00:00:55,690 --> 00:00:58,930 that is perfectly possible without transactions. 13 00:00:58,930 --> 00:01:06,700 Now the thing just is what happens when we actually have a use case where deleting the user succeeds 14 00:01:07,030 --> 00:01:11,070 but during deleting the posts, somehow something goes wrong? 15 00:01:11,170 --> 00:01:13,360 We have a temporary server outage, 16 00:01:13,390 --> 00:01:15,580 we have a network issue, whatever it is. 17 00:01:15,580 --> 00:01:22,960 So then we end up in a state where the user was deleted but the posts are still there but the user they're 18 00:01:22,960 --> 00:01:25,700 pointing at doesn't exist anymore, 19 00:01:26,200 --> 00:01:30,440 this is exactly the use case where transactions help us. With a transaction, 20 00:01:30,520 --> 00:01:37,120 we can basically tell mongodb hey these operations as many as you want either succeed together 21 00:01:37,270 --> 00:01:40,680 or they fail together and you roll back, 22 00:01:40,750 --> 00:01:46,810 so you restore the database in the state it was in before the transaction regarding the documents 23 00:01:46,930 --> 00:01:49,130 that were affected in the transaction. 24 00:01:49,240 --> 00:01:51,220 That is the idea behind transactions, 25 00:01:51,220 --> 00:01:54,500 let's now try that out and again for that, you need mongodb 4 26 00:01:54,610 --> 00:01:59,510 and a replica set, so that is why I will use the mongodb Atlas deployment 27 00:01:59,530 --> 00:02:01,060 we created in the last module.