1 00:00:02,130 --> 00:00:08,410 Back in the shell and still connected to that same mongodb instance we used earlier, 2 00:00:08,640 --> 00:00:13,000 let's again check out our flight data collection with find 3 00:00:13,260 --> 00:00:17,350 and as you saw, use find pretty to get a pretty output, 4 00:00:17,400 --> 00:00:19,280 you see we got three documents in there. 5 00:00:19,350 --> 00:00:21,880 We got the documents with our own custom ID, 6 00:00:22,140 --> 00:00:29,220 we got the other document with incomplete information but the automatically generated ID and our original 7 00:00:29,220 --> 00:00:32,560 document, we already saw insertOne. 8 00:00:32,670 --> 00:00:39,690 Now let me actually start with clearing this database now before we then insert data again but this 9 00:00:39,690 --> 00:00:48,810 time with insertMany. Now to clear the database, we have to use a delete command and we can simply use 10 00:00:48,810 --> 00:00:54,120 our flight data collection because all these commands are always executed on a collection where you 11 00:00:54,120 --> 00:01:02,680 want to add or delete documents and there, we got deleteOne or deleteMany. 12 00:01:02,700 --> 00:01:09,270 Now here I want to clear all the data and for this, actually the easiest way would be to simply get rid 13 00:01:09,630 --> 00:01:11,120 of the entire collection 14 00:01:11,370 --> 00:01:13,090 but since that would be boring, 15 00:01:13,200 --> 00:01:17,200 let's go with delete one to simply see how that works. 16 00:01:17,260 --> 00:01:21,240 Delete one takes a filter to find out which document to delete 17 00:01:21,510 --> 00:01:23,970 and now we could filter for all kinds of things. 18 00:01:24,030 --> 00:01:26,000 A filter is defined as a document, 19 00:01:26,010 --> 00:01:32,910 so with curly braces and then in its simplest form, you now simply define which key and which value 20 00:01:32,910 --> 00:01:38,190 you want to delete or the document with that key and value you want to delete. 21 00:01:38,190 --> 00:01:43,480 So for example here, we could say departure airport, 22 00:01:43,640 --> 00:01:50,310 if that is equal to txl so to Berlin tegle, then I want to delete that document 23 00:01:50,450 --> 00:01:57,650 and what this would do is it would go ahead, find the first document in our database where the departure 24 00:01:57,650 --> 00:02:00,550 airport is txl and delete it. 25 00:02:00,550 --> 00:02:04,340 So if I hit enter here, you see this was accepted, it was successful 26 00:02:04,430 --> 00:02:06,960 and we see it deleted one document. 27 00:02:07,340 --> 00:02:16,330 So if we now again use our flight data and find all documents and pretty print them, we only see two documents 28 00:02:16,360 --> 00:02:20,510 now, the one with our custom ID and the first document we ever entered. 29 00:02:20,590 --> 00:02:26,070 This means that this middle document was now deleted because it was the first document with the departure 30 00:02:26,080 --> 00:02:28,140 airport of txl which it found, 31 00:02:28,510 --> 00:02:31,570 obviously we don't have to use the departure airport, 32 00:02:31,570 --> 00:02:41,510 we could have looked for an ID too. So I could also use db.flightdata.deleteOne and I could have also 33 00:02:41,510 --> 00:02:46,330 looked for _ID and then maybe my custom ID, this one here, 34 00:02:46,360 --> 00:02:48,510 txl-lhr-1, 35 00:02:48,710 --> 00:02:55,040 if I do this, it would delete this document. But let me also show you how to delete many documents. 36 00:02:55,040 --> 00:02:58,050 Now the thing is we got nothing in common, 37 00:02:58,130 --> 00:03:02,550 if I just execute deleteMany like this, it fails though, 38 00:03:02,570 --> 00:03:05,120 so I need to pass in a criteria. 39 00:03:05,120 --> 00:03:08,690 Now we got nothing in common for our elements here, 40 00:03:08,750 --> 00:03:16,070 so let's first of all update them real quick to add a new field to both documents so that they have something 41 00:03:16,070 --> 00:03:21,010 in common, so that we can delete them in bulk, so that I can also show you this. 42 00:03:21,020 --> 00:03:29,890 So for this, let's go back to the collection and now use update one, update one updates as the name suggests, one 43 00:03:30,070 --> 00:03:37,430 element and for update one, we first of all define a filter where we say which document should be updated 44 00:03:37,520 --> 00:03:41,530 and now let's say we want to start with this first element here. 45 00:03:41,530 --> 00:03:45,410 So I want to update the document where in my case here, 46 00:03:45,520 --> 00:03:47,230 maybe distance is 12000, 47 00:03:47,230 --> 00:03:49,500 this should select this first document. 48 00:03:49,720 --> 00:03:52,580 The second argument then is how I want to change it 49 00:03:52,990 --> 00:03:55,660 and now the question is what do you pass there. 50 00:03:55,660 --> 00:03:59,310 Now let's say we want to add a new field 51 00:04:00,540 --> 00:04:08,620 and therefore we pass a document here, marker which hold some text, delete. By the way as you see here, you 52 00:04:08,620 --> 00:04:11,490 can use single quotation marks for the value too 53 00:04:11,530 --> 00:04:14,210 but you can also use double quotation marks. 54 00:04:14,280 --> 00:04:21,370 Now this might add a new field marker with the value delete to that first element 55 00:04:21,370 --> 00:04:27,220 but if I hit enter, I actually get an error and the error is that the update operation document must contain 56 00:04:27,460 --> 00:04:31,110 an atomic operator or atomic operators. 57 00:04:31,120 --> 00:04:33,030 Now this is a very valid error 58 00:04:33,070 --> 00:04:37,240 and there's not really much you can do with it if you're just starting with mongodb. 59 00:04:37,240 --> 00:04:41,740 The thing is it simply is an incorrect type of data for updating, 60 00:04:41,740 --> 00:04:47,410 you don't pass in a document describing your change like this because Mongodb doesn't know how to interpret 61 00:04:47,470 --> 00:04:48,310 this, 62 00:04:48,340 --> 00:04:55,120 instead what you do pass here is a document with curly braces with a special keyword, $set 63 00:04:55,600 --> 00:05:02,290 and this is something you can remember, whenever you see something with dollar sign in mongodb, it's 64 00:05:02,290 --> 00:05:09,580 a reserved operator or word, $set is simply identified by mongodb when used in the update 65 00:05:09,580 --> 00:05:13,280 one operation to describe the changes you want to make. 66 00:05:13,540 --> 00:05:19,840 The value of $set then is a document with curly braces and we need to close another pair 67 00:05:19,840 --> 00:05:26,810 of curly braces for our surrounding curly braces now marked in purple and now what this does is it tells 68 00:05:26,810 --> 00:05:30,460 mongodb hey for this document you're finding, 69 00:05:30,460 --> 00:05:38,710 please set this value and if this value did exist, it would change it to delete and if it does not exist 70 00:05:38,740 --> 00:05:40,930 as in our case, it will add it. 71 00:05:40,950 --> 00:05:43,200 So if I now hit enter, this succeeded, 72 00:05:43,240 --> 00:05:49,840 you see it found one document that matched our filter and it modified one document then and now if 73 00:05:49,840 --> 00:05:54,970 I again access my flight data, find it and pretty print it like this, 74 00:05:54,970 --> 00:05:59,150 we see we now added the marker here. 75 00:05:59,170 --> 00:06:04,570 Now let's say I want to add that marker to all my documents, 76 00:06:04,570 --> 00:06:13,650 now what you can do then is you can use update many, so on flight data, you can use update many 77 00:06:14,020 --> 00:06:19,360 and now you need to pass something as a filter but you can simply pass empty curly braces 78 00:06:19,540 --> 00:06:22,280 and we could have done that on delete many too by the way, 79 00:06:22,330 --> 00:06:29,230 this will select all documents. And now here, I again use set and then describe what to change 80 00:06:29,300 --> 00:06:36,860 and now let's say I'll set the marker to toDelete here, so not delete but toDelete. If you hit enter, 81 00:06:36,860 --> 00:06:40,790 you see it matched two documents and it modified two documents 82 00:06:41,040 --> 00:06:47,250 and now if you again access your flight data, you find it and you pretty print it, you see that both 83 00:06:47,250 --> 00:06:49,110 now have the marker to delete. 84 00:06:49,110 --> 00:06:55,590 Please also note that for the first document, this was delete before and it was changed to toDelete 85 00:06:57,050 --> 00:06:59,900 and now with that, we can use that marker to delete all. 86 00:06:59,900 --> 00:07:07,950 Now as I mentioned, what we could do here is we could also use delete many and pass an empty pair of 87 00:07:08,040 --> 00:07:09,030 curly braces too, 88 00:07:09,030 --> 00:07:14,160 this should delete all elements but of course I also wanted to show you how to use update, 89 00:07:14,250 --> 00:07:20,860 so here I will now use my new condition and check for all elements where the marker is to delete. 90 00:07:21,060 --> 00:07:27,840 If we now hit enter, you see this was accepted and two elements were deleted and now if I access flight data 91 00:07:28,320 --> 00:07:32,030 and I use find and pretty print this, well 92 00:07:32,040 --> 00:07:35,330 we get back nothing because there is nothing in there anymore. 93 00:07:35,670 --> 00:07:41,040 So this is how we can insert elements, how we can delete and update them and how we find them 94 00:07:41,220 --> 00:07:43,190 but it's not all I wanted to show you. 95 00:07:43,260 --> 00:07:44,610 Let's continue in the next lecture.