1 00:00:02,150 --> 00:00:08,550 With those basics about the data, the documents and how we work with data out of the way, 2 00:00:08,810 --> 00:00:15,970 let's shift our focus towards the crud operations and how we work with the different crud operations 3 00:00:15,980 --> 00:00:17,840 we may use in mongodb. 4 00:00:17,840 --> 00:00:20,830 Now first of all, what is crud all about, 5 00:00:20,860 --> 00:00:22,520 why does this matter? 6 00:00:22,550 --> 00:00:28,550 You can use mongodb for a broad variety of things, you might be building an application or you 7 00:00:28,550 --> 00:00:34,570 have some data which is shipped to you as one file and you just want to run some analytics on it or you're 8 00:00:34,610 --> 00:00:38,450 an administrator of a database, of a mongo database, 9 00:00:38,630 --> 00:00:42,170 in all these cases, you will have to interact with the database. 10 00:00:42,350 --> 00:00:48,320 Now in the application case, you might have an app, a mobile app or a website where user interacts 11 00:00:48,320 --> 00:00:50,440 with your code in the end 12 00:00:50,570 --> 00:00:57,470 and that code can be written in nodejs, php, can be a desktop app written in C++, 13 00:00:57,530 --> 00:01:03,800 really no limits there and you will have your mongodb driver included in your application then. In 14 00:01:04,080 --> 00:01:05,870 the other cases like for analytics, 15 00:01:05,870 --> 00:01:12,260 you might be using the business intelligence connector mongodb offers or you directly use the shell 16 00:01:12,290 --> 00:01:18,830 or some other import mechanism provided by your favorite BI tool or as an administrator, you'll work 17 00:01:18,830 --> 00:01:23,690 with the shell like we're doing it right now. In all these cases, you want to interact with your mongodb 18 00:01:23,690 --> 00:01:27,180 server and for an application like a website, 19 00:01:27,200 --> 00:01:33,430 you typically want to be able to create elements, you want to be able to read, update and delete them. 20 00:01:33,560 --> 00:01:34,860 Let's say you were building a blog, 21 00:01:34,880 --> 00:01:39,760 you want to be able to create new posts, to deliver those posts to your users, 22 00:01:39,800 --> 00:01:42,010 you want to be able to update these posts, 23 00:01:42,050 --> 00:01:48,980 so to change them or maybe even delete them. With analytics, you also at least want to be able to read 24 00:01:48,980 --> 00:01:49,640 the data, 25 00:01:49,640 --> 00:01:52,490 you might never change it but you want to read it 26 00:01:52,730 --> 00:01:58,640 and as an admin, you might want to do all these things to fix some data or to play around with it as 27 00:01:58,640 --> 00:01:59,820 we are doing it. 28 00:01:59,840 --> 00:02:02,520 So these are these core crud operations 29 00:02:02,600 --> 00:02:06,510 and in the end, these operations are the only thing you do with your data, 30 00:02:06,530 --> 00:02:10,740 this is what you have your data for, to create it, manage it, read it, 31 00:02:10,820 --> 00:02:14,560 all these things and you do these things with the mongodb server 32 00:02:14,690 --> 00:02:22,450 and there of course, in the end on a collection and the documents in that collection. Now for these crud operations, 33 00:02:22,600 --> 00:02:29,970 we have a couple of important queries you should keep in mind and you'll also find in the official docs 34 00:02:29,970 --> 00:02:34,980 of course in case you want to check out all the details or dive a bit deeper. 35 00:02:35,160 --> 00:02:37,430 There is insertOne which we already saw, 36 00:02:37,590 --> 00:02:43,860 insertOne allows us to insert some data into a collection and all these methods I'm showing you are 37 00:02:43,860 --> 00:02:48,610 directly executed on a collection as we did it in the last lectures 38 00:02:49,050 --> 00:02:52,900 and we can also pass some options along with insertOne, 39 00:02:53,040 --> 00:02:54,760 we'll not to worry about that right now, 40 00:02:54,810 --> 00:02:57,690 we'll dive into that later though. 41 00:02:57,690 --> 00:03:02,250 Besides inserting one document, we can also insert many documents by the way, 42 00:03:02,310 --> 00:03:07,470 so if you have a use case where you need to insert more than one document at a time, you can do that 43 00:03:07,530 --> 00:03:08,410 too, 44 00:03:08,940 --> 00:03:12,600 this is important for creating data. Now for reading data, 45 00:03:12,600 --> 00:03:17,830 we got find and find also allows us to pass in some arguments. 46 00:03:17,850 --> 00:03:21,940 Right now we're calling it without anything, just find empty parentheses, 47 00:03:22,140 --> 00:03:26,170 now you can pass in a filter and filters are very powerful, 48 00:03:26,190 --> 00:03:30,710 they allow you to narrow down the data you're retrieving, something you of course often want to do in 49 00:03:30,720 --> 00:03:34,550 a database and you can also pass some options to find 50 00:03:34,800 --> 00:03:40,040 and again, we'll take a close look at all these options and filters throughout the course too. 51 00:03:40,500 --> 00:03:42,140 You also got find one, 52 00:03:42,180 --> 00:03:49,140 the difference here is that find finds all matching documents, find one simply gives you the first matching 53 00:03:49,140 --> 00:03:55,900 document it finds. Update one is then useful for changing one piece of data 54 00:03:56,070 --> 00:03:59,330 and just as we can insert many documents at once, 55 00:03:59,370 --> 00:04:06,540 we also have update many. Now the update functions take three arguments because we can pass a filter to 56 00:04:06,540 --> 00:04:12,570 narrow down which document or documents to change then the data describing the change, 57 00:04:12,570 --> 00:04:14,110 so what should be changed 58 00:04:14,220 --> 00:04:18,350 and again some options we can use to configure that process. 59 00:04:18,360 --> 00:04:24,450 We also got replace one in case we just want to replace a document entirely with a new one. 60 00:04:26,030 --> 00:04:27,570 There also is delete, 61 00:04:27,770 --> 00:04:34,610 we can delete one or delete many documents in a collection and there we also pass in a filter to find 62 00:04:34,610 --> 00:04:37,640 out which document or documents to delete. 63 00:04:37,640 --> 00:04:40,490 Now let's see all these things at an example, 64 00:04:40,700 --> 00:04:44,660 again having a look at our flight data here which could look like this, 65 00:04:44,660 --> 00:04:52,430 basically the data we already worked with and there, let's say this is the data we're using on our application. 66 00:04:52,490 --> 00:04:59,330 Our application might be a website where people working at an airport or at an airline can schedule 67 00:04:59,480 --> 00:05:00,680 a new flight, 68 00:05:00,770 --> 00:05:04,940 therefore they would create an entry in our flight data collection, 69 00:05:05,030 --> 00:05:09,350 they can change information about this flight so they can update information, 70 00:05:09,350 --> 00:05:16,190 for example if the aircraft changed, they can cancel the flight let's say so they can delete it and 71 00:05:16,220 --> 00:05:21,830 they can also display flight information on their own website in the mobile apps for their passengers 72 00:05:22,700 --> 00:05:27,480 and therefore, they need to be able or you need to be able to read the data from this collection. 73 00:05:27,500 --> 00:05:31,070 This is our little example here using the data we already set up. 74 00:05:31,100 --> 00:05:35,390 So let's get started and play around with this crud operations and our data.