﻿1
00:00:01,220 --> 00:00:02,299
‫Finally, let's learn

2
00:00:02,299 --> 00:00:05,463
‫how to delete documents in MongoDB.

3
00:00:06,790 --> 00:00:08,160
‫So just like before,

4
00:00:08,160 --> 00:00:11,641
‫we have delete one to delete one single document,

5
00:00:11,641 --> 00:00:14,290
‫and we have delete many, well,

6
00:00:14,290 --> 00:00:17,430
‫to delete multiple documents at the same time.

7
00:00:17,430 --> 00:00:19,430
‫Alright, and just like before,

8
00:00:19,430 --> 00:00:21,250
‫delete one will only work

9
00:00:21,250 --> 00:00:24,220
‫for the first document matching your query.

10
00:00:24,220 --> 00:00:25,800
‫And delete many will of course

11
00:00:25,800 --> 00:00:28,600
‫work for all the documents matching your query.

12
00:00:28,600 --> 00:00:29,796
‫Okay.

13
00:00:29,796 --> 00:00:33,660
‫Just to get an overview of what we have here

14
00:00:33,660 --> 00:00:35,890
‫at this moment in time,

15
00:00:35,890 --> 00:00:39,760
‫let's do a find of all the tours.

16
00:00:39,760 --> 00:00:40,830
‫And what I'm gonna do now

17
00:00:40,830 --> 00:00:42,330
‫is to delete all the tours

18
00:00:42,330 --> 00:00:44,923
‫which have a rating less than 4.8.

19
00:00:45,987 --> 00:00:48,172
‫So that's gonna be just the first one

20
00:00:48,172 --> 00:00:51,491
‫but if we had multiple tours with that condition,

21
00:00:51,491 --> 00:00:54,030
‫then all of them would be deleted.

22
00:00:54,030 --> 00:00:57,050
‫So db.tours.deleteMany.

23
00:01:01,843 --> 00:01:04,260
‫So the condition is rating...

24
00:01:06,750 --> 00:01:08,460
‫Open a new object

25
00:01:08,460 --> 00:01:13,460
‫so that we can use the less than operator and 4.8.

26
00:01:13,680 --> 00:01:17,500
‫Close it and then close this one.

27
00:01:17,500 --> 00:01:22,040
‫And run this and here we see that it's deleted one document.

28
00:01:22,040 --> 00:01:23,620
‫Right away it's telling us here

29
00:01:23,620 --> 00:01:26,573
‫how many documents were affected by this operation.

30
00:01:27,440 --> 00:01:29,240
‫So if we do this find now,

31
00:01:29,240 --> 00:01:32,680
‫well then the first one that we had is gone.

32
00:01:32,680 --> 00:01:34,610
‫Alright, so that's no problem.

33
00:01:34,610 --> 00:01:37,637
‫We are only really using this here to play around

34
00:01:37,637 --> 00:01:40,640
‫and so we can delete as much as we want here.

35
00:01:40,640 --> 00:01:41,760
‫No problem.

36
00:01:41,760 --> 00:01:42,980
‫Now if you wanna delete

37
00:01:42,980 --> 00:01:45,180
‫all of the documents in the collection,

38
00:01:45,180 --> 00:01:46,543
‫you would do it like this.

39
00:01:48,920 --> 00:01:50,818
‫So deleteMany.

40
00:01:50,818 --> 00:01:52,360
‫And I'm not gonna execute this.

41
00:01:52,360 --> 00:01:54,130
‫But you would do it like this.

42
00:01:54,130 --> 00:01:57,059
‫So all you have to do is pass in the empty object

43
00:01:57,059 --> 00:02:00,280
‫because the empty object is basically a condition

44
00:02:00,280 --> 00:02:03,196
‫that all of the documents always match.

45
00:02:03,196 --> 00:02:08,143
‫It would be like simply using an empty object here in find.

46
00:02:09,223 --> 00:02:11,225
‫I'm not gonna do that

47
00:02:11,225 --> 00:02:13,393
‫and you should be careful with it,

48
00:02:13,393 --> 00:02:16,760
‫especially when doing it in a real app

49
00:02:16,760 --> 00:02:19,690
‫because you cannot come back from this operation.

50
00:02:19,690 --> 00:02:21,580
‫So unless you have some backup in place,

51
00:02:21,580 --> 00:02:23,686
‫this will effectively delete all the documents

52
00:02:23,686 --> 00:02:25,350
‫in your collection,

53
00:02:25,350 --> 00:02:27,170
‫in this case the tours collection.

54
00:02:27,170 --> 00:02:29,460
‫So I'm not performing that.

55
00:02:29,460 --> 00:02:30,730
‫I just wanted to show you

56
00:02:30,730 --> 00:02:32,810
‫that this is how we delete everything.

57
00:02:32,810 --> 00:02:34,440
‫So this is the simplest operation

58
00:02:34,440 --> 00:02:36,450
‫and we don't use it all that often.

59
00:02:36,450 --> 00:02:38,740
‫But I just wanted to have a short video

60
00:02:38,740 --> 00:02:41,383
‫just to demonstrate that it also exists.

