So here I am connected to that mongodb Atlas server in the web
and I will use a new database,
blog and in there, let's quickly insert some dummy data.
Let's insert a users collection and I'll insert one user here,
I'll make this really simple,
I'll have a user with the name of Max. That is my one user,
now I got a user ID and I will copy that ID because now I'll add a posts collection where I insert many
posts, therefore I need an array here
and then the first post has a title of first post and important, a user ID at which it points,
so here I'll use a relation with references.
Check out the relations module in this course if you want to learn more about data schemas and relations.
So here I'll just store that ID and then I'll add a second post here with a title of second post and
that post will have been created by the same user,
so I store the same ID as a value here.
If I hit enter, these two posts are also created.
Now if we want to delete the user, we would obviously find out the user id and that is probably something
you know in the application where the user clicked on delete my account,
so in the end you would do something like db users delete one and then search for _id equal
to that user ID which you know. Now you would do that
and then with that user ID which you do know, you would of course also go ahead and also execute db
products delete many and delete all products where the user ID,
so that field which I added to each post where we point at the user is equal to the user id too.
I won't execute these commands because I don't want to delete the data right now but this is how we
could clear all the data and this would work in 99.9%
of cases. The problem just is the cases where it doesn't work because something goes wrong and
we end up in a state where it suddenly, well we deleted the user but not the posts or the other way around
and for that, we can work with transactions.
Now how does a transaction work?
Let's dive into that in the next lecture.