1 00:00:02,120 --> 00:00:08,020 So back in our mongo shell with the data I also worked on in the last modules. 2 00:00:08,090 --> 00:00:11,550 Now for what I'm showing you, you can use with any data you have, 3 00:00:11,570 --> 00:00:13,260 you don't need specific data. 4 00:00:13,610 --> 00:00:19,810 So here I happen to still have my users collection I worked with in the update module 5 00:00:19,850 --> 00:00:25,700 but again it's fine if you have different data or the same collection but the data looks different, that's 6 00:00:25,700 --> 00:00:26,790 all fine. 7 00:00:27,320 --> 00:00:30,490 Let's say we want to delete Chris, now 8 00:00:30,530 --> 00:00:31,290 to delete 9 00:00:31,310 --> 00:00:38,270 Chris, what you can do is you can use your collection and then use delete one if you know it'll be only 10 00:00:38,420 --> 00:00:40,790 one document you want to delete. 11 00:00:40,910 --> 00:00:47,870 Now you need to specify a query selector, a filter here and the filter you specify here is the same as 12 00:00:47,870 --> 00:00:50,230 for updating and finding documents, 13 00:00:50,370 --> 00:00:57,030 everything you learned in the read module will work here, whatever you need to identify a document, with 14 00:00:57,170 --> 00:01:00,510 how many conditions you might need is fine. 15 00:01:00,620 --> 00:01:07,310 If you check the age and the name and let's say the hobbies all in one query selection, that is fine, 16 00:01:07,310 --> 00:01:08,300 you can do that, 17 00:01:08,450 --> 00:01:13,950 you simply need to narrow down the document or the documents you want to delete. 18 00:01:13,970 --> 00:01:16,400 So a simple check is of course to look for 19 00:01:16,460 --> 00:01:19,820 name equal to Chris and that is it, 20 00:01:19,820 --> 00:01:26,860 you can specify a second argument where you can set a write concern if you want to but this is not required 21 00:01:26,870 --> 00:01:28,370 if you're fine with the default write 22 00:01:28,370 --> 00:01:32,930 concern as I am for the rest of this course. So I can just hit enter 23 00:01:33,110 --> 00:01:35,900 and you see, one document was deleted 24 00:01:36,380 --> 00:01:44,480 and unsurprisingly if we now look into all users, we'll see Maria, Anna, Manuel and Max but Chris is 25 00:01:44,480 --> 00:01:45,090 gone. 26 00:01:46,430 --> 00:01:49,900 Now with Chris being gone and delete one being used, 27 00:01:50,090 --> 00:01:59,880 let me use delete many to delete all persons who are sporty and have the age greater than 30. 28 00:01:59,960 --> 00:02:04,260 So for this, we can again use our collection and then delete many 29 00:02:04,280 --> 00:02:07,260 because here I know I want to delete multiple records, 30 00:02:07,280 --> 00:02:12,160 this can still delete only one if only one record should match your condition 31 00:02:12,380 --> 00:02:18,530 but unlike delete one which will then only just delete the first record that matches, even if you had 32 00:02:18,530 --> 00:02:20,340 multiple documents that would match, 33 00:02:20,480 --> 00:02:21,370 this delete 34 00:02:21,380 --> 00:02:25,330 many will always delete all matching documents. 35 00:02:25,400 --> 00:02:33,180 So here I can say age should be greater than 30 and isSporty 36 00:02:33,270 --> 00:02:34,420 should be true, 37 00:02:34,740 --> 00:02:36,810 this is now my condition. 38 00:02:36,810 --> 00:02:38,280 If I hit enter, 39 00:02:38,280 --> 00:02:40,040 nothing was deleted 40 00:02:40,300 --> 00:02:41,910 so let's quickly check, 41 00:02:42,030 --> 00:02:44,880 well it's total age of course, not age, 42 00:02:44,970 --> 00:02:45,770 my bad, 43 00:02:45,990 --> 00:02:51,220 so let's quickly change this to total age and still, nothing was deleted. 44 00:02:51,540 --> 00:02:52,980 Well the reason for this is simple, 45 00:02:53,010 --> 00:02:55,090 we had no matching document. 46 00:02:55,140 --> 00:03:04,720 Maria is simply younger, Anna doesn't have an age, Manuel would have an age but is not sporty and Max is 47 00:03:04,730 --> 00:03:06,600 sporty but doesn't have an age. 48 00:03:06,600 --> 00:03:13,090 So what we can do is we can of course delete all persons who are sporty and where total 49 00:03:13,110 --> 00:03:15,030 age does not exist, 50 00:03:15,150 --> 00:03:19,130 so where exists is false, 51 00:03:19,170 --> 00:03:21,950 you learned about exists in the read section too. 52 00:03:21,960 --> 00:03:25,450 Now if I hit enter, two documents were deleted, 53 00:03:25,560 --> 00:03:30,270 so let's quickly have a look at our collection and 54 00:03:30,270 --> 00:03:35,700 we see Anna is still there because she doesn't have an age, she's sporty but she doesn't have an age 55 00:03:35,880 --> 00:03:36,740 and total age 56 00:03:36,760 --> 00:03:41,200 null as you learn then the read section does not suffice for exists 57 00:03:41,200 --> 00:03:43,800 false because the field does exist, 58 00:03:43,840 --> 00:03:46,400 it's null but it does exist. 59 00:03:46,410 --> 00:03:48,270 Manuel is also still there 60 00:03:48,270 --> 00:03:54,960 but Max and Maria were deleted because they were sporty and they had no age field 61 00:03:54,960 --> 00:03:56,220 by the looks of it. 62 00:03:56,910 --> 00:03:58,670 So this is delete many in action 63 00:03:58,770 --> 00:04:03,870 and as you see, here delete many taking into account two different query selectors, 64 00:04:03,870 --> 00:04:05,940 of course you can add as many as you need 65 00:04:05,940 --> 00:04:06,540 as I mentioned.