1
00:00:00,180 --> 00:00:06,000
Beautiful, and once we're familiar with Virtualize now, I want to show you, finally, why are we

2
00:00:06,000 --> 00:00:06,900
used here?

3
00:00:06,930 --> 00:00:12,870
This dot remove so in the product, why were used the bathroom?

4
00:00:13,530 --> 00:00:21,030
And the idea is following we have the product correct and we have some reviews associated with that

5
00:00:21,030 --> 00:00:21,450
product.

6
00:00:22,400 --> 00:00:26,000
But what happens when we remove the product?

7
00:00:26,630 --> 00:00:33,800
Does it make sense that we have some kind of revenues laying around, even though they're not associated

8
00:00:34,160 --> 00:00:35,720
with any type of product?

9
00:00:36,410 --> 00:00:43,640
And of course, just like with all my questions, very, very obvious answer is no, we don't want that.

10
00:00:44,120 --> 00:00:52,130
And how we can accomplish that, well, we could set up a perimeter, a move hook basically before we

11
00:00:52,130 --> 00:00:53,240
remove the product.

12
00:00:53,510 --> 00:01:02,000
I want to go to a review model, get the reviews that are associated with the specific product and just

13
00:01:02,000 --> 00:01:02,570
remove them.

14
00:01:02,990 --> 00:01:10,190
And if you remember when we were seeding the database, we use this delete many, not exactly the method

15
00:01:10,460 --> 00:01:11,420
we're going to use.

16
00:01:11,960 --> 00:01:20,330
And just like in the previous examples, the reason why we're using here is remove because of this will

17
00:01:20,690 --> 00:01:22,370
trigger that hook.

18
00:01:23,290 --> 00:01:30,340
So unlike the fine wine elite, it wouldn't trigger, but with this remove, it triggers the hook and

19
00:01:30,340 --> 00:01:31,900
therefore we have it here.

20
00:01:32,350 --> 00:01:41,590
So now let's go to the product model or it is right over here and then right below the vertical, we're

21
00:01:41,590 --> 00:01:44,700
going to go with product schema vanguard.

22
00:01:45,190 --> 00:01:47,020
And then we go to pre now.

23
00:01:47,020 --> 00:01:48,760
In this case, though, it's not pretty safe.

24
00:01:49,180 --> 00:01:50,920
We're going to go with pre remove.

25
00:01:51,680 --> 00:01:54,720
And in here, it's going to be a sync function.

26
00:01:55,690 --> 00:02:01,370
Again, will go with good old function word, because where you want to access that venerable go forward

27
00:02:01,390 --> 00:02:04,480
next year, if you remember this was technically optional.

28
00:02:04,840 --> 00:02:09,639
With the latest versions of Mongoose, we don't actually need to call that, but let's just leave it

29
00:02:09,639 --> 00:02:09,970
there.

30
00:02:10,370 --> 00:02:13,060
And in this case, I want to go with a wait.

31
00:02:13,420 --> 00:02:14,890
This is going to be asynchronous.

32
00:02:15,340 --> 00:02:16,560
And what's really cool?

33
00:02:16,570 --> 00:02:24,860
Not only I can access this model by using this actually have a method there by the name of model.

34
00:02:25,240 --> 00:02:29,170
Now in the model, I can pass in a different model.

35
00:02:29,530 --> 00:02:31,120
Now what is the model that I'm looking for?

36
00:02:31,150 --> 00:02:37,030
Well, when I'm removing the product, I want to access the review model, correct?

37
00:02:37,090 --> 00:02:38,830
So and here we type review.

38
00:02:39,400 --> 00:02:46,480
Then we go with the method name and we go with delete, delete and many now in delete.

39
00:02:46,480 --> 00:02:51,400
Many we want to specifically say, Well, what review is we want to remove?

40
00:02:51,730 --> 00:02:55,810
And we want to remove the reviews were product matches.

41
00:02:55,810 --> 00:02:58,090
What was that underscore score?

42
00:02:58,450 --> 00:03:03,040
Now we cannot see c underscore idea, but we know that when every document there is that property.

43
00:03:03,430 --> 00:03:08,110
So go here would product is equal to this underscore.

44
00:03:08,680 --> 00:03:15,070
If you're wondering why we go here with the product, because that's the property on the review model

45
00:03:15,370 --> 00:03:16,990
that references the product.

46
00:03:18,100 --> 00:03:24,490
Like I said, we go here with this on the score and I.D. We share it.

47
00:03:24,850 --> 00:03:28,810
Let me try without next and let's see how it works out.

48
00:03:29,320 --> 00:03:33,430
So now I'm going to head back to the postman again.

49
00:03:33,430 --> 00:03:40,000
I'm going to get all the reviews and they are associated with only one product.

50
00:03:41,220 --> 00:03:48,930
So first, I guess I need to organize John, correct, because only admins have those privileges, so

51
00:03:48,930 --> 00:03:49,980
I'm going to go with John.

52
00:03:51,080 --> 00:03:53,600
I log in and then.

53
00:03:54,800 --> 00:03:56,570
Let me get this, Heidi.

54
00:03:57,410 --> 00:03:58,340
That's the product.

55
00:03:59,060 --> 00:04:00,260
It's very, very important.

56
00:04:01,760 --> 00:04:03,710
Now I want to remove the product.

57
00:04:04,130 --> 00:04:05,510
So let me go back here.

58
00:04:06,140 --> 00:04:07,100
Let me remove it.

59
00:04:07,130 --> 00:04:10,910
Let me send and I successfully remove the product.

60
00:04:11,900 --> 00:04:18,709
And here comes the moment of truth, whether I see any reviews because both reviews were associated

61
00:04:18,709 --> 00:04:26,600
with that, and once I said I have no reviews, which means that the logic works once we remove the

62
00:04:26,600 --> 00:04:27,200
product.

63
00:04:27,920 --> 00:04:35,930
Right before we do that, we go to reviews and we're delete all the reviews that are associated with

64
00:04:36,170 --> 00:04:37,500
this particular product.

65
00:04:37,520 --> 00:04:44,690
And again, I know I'm repeating myself, but that was the deal why we needed to use here this hour

66
00:04:44,690 --> 00:04:49,940
wait product or move because this one will trigger the hook.

67
00:04:50,630 --> 00:04:51,880
Hopefully, that's clear.

68
00:04:52,240 --> 00:04:58,970
And now we're familiar of how we can remove all the reviews associated with specific product.

69
00:04:59,300 --> 00:05:01,040
Once we delete the product.

