1 00:00:02,130 --> 00:00:07,060 We already covered a lot about how to retrieve data, how to store data, update and delete it 2 00:00:07,140 --> 00:00:09,540 and we're nearing the end of this module 3 00:00:09,780 --> 00:00:14,690 but before we conclude the module, there are two other important things you gotta know 4 00:00:14,880 --> 00:00:17,400 and the first one is projection. 5 00:00:17,400 --> 00:00:19,430 Now what is projection? 6 00:00:19,440 --> 00:00:24,130 Imagine you have this kind of data in your database, some information about a person, 7 00:00:24,150 --> 00:00:27,740 in this case me with name, age, job and that ID. 8 00:00:27,840 --> 00:00:29,420 Now imagine in your application, 9 00:00:29,430 --> 00:00:30,880 you don't need all this data, 10 00:00:30,900 --> 00:00:34,930 maybe you just need the name and age for a given page on your website. 11 00:00:35,010 --> 00:00:40,170 Of course you could fetch all the data and filter it in your application, in any programming language 12 00:00:40,170 --> 00:00:46,080 you can of course manipulate the data you're getting back but you would still transfer it over to the 13 00:00:46,080 --> 00:00:46,860 wire, 14 00:00:46,860 --> 00:00:53,430 so you would kind of impact your bandwidth, you would fetch unnecessary data and you want to prevent this, 15 00:00:53,430 --> 00:00:57,130 so it would be better to filter this out on the mongodb server 16 00:00:57,300 --> 00:01:02,560 and this is exactly what projection can help you with. Projection is super easy to use, 17 00:01:02,580 --> 00:01:04,060 let me show it to you. 18 00:01:04,110 --> 00:01:10,470 We've got our passengers and on the passengers, let's do a quick find again, here are our passengers and 19 00:01:10,470 --> 00:01:13,900 now let's say I'm only interested in the names of the passengers. 20 00:01:14,040 --> 00:01:19,020 Now you can do projection by using find, 21 00:01:19,110 --> 00:01:24,780 now you need to pass a first argument because I actually want to pass a second argument and that first 22 00:01:24,780 --> 00:01:30,090 argument now is an empty document because I want to find all passengers and I don't want to pass a filter 23 00:01:30,420 --> 00:01:38,430 and the second argument now allows us to project. And here, the projection is set up by passing another 24 00:01:38,430 --> 00:01:39,070 document, 25 00:01:39,090 --> 00:01:41,440 so another pair of curly braces 26 00:01:41,460 --> 00:01:46,010 and here you simply specify which key value pairs you want to get. 27 00:01:46,120 --> 00:01:49,110 For that, you simply name the field you want to get, 28 00:01:49,120 --> 00:01:53,560 so let's say in our passengers, we have a name and we want to get that name, 29 00:01:53,650 --> 00:01:57,030 so we type name here and then as a value, simply a one, 30 00:01:57,040 --> 00:02:05,140 this means include it in the data you're returning to me. If we now output that, we get all our fields 31 00:02:05,140 --> 00:02:08,560 with just a name and well still the ID, 32 00:02:08,770 --> 00:02:13,810 so the age is missing and the age is of course not missing in a database but only in the data we retrieved 33 00:02:13,810 --> 00:02:14,590 from it, 34 00:02:14,590 --> 00:02:15,930 so in the data we're showing here. 35 00:02:15,940 --> 00:02:18,170 So this is projection doing its job 36 00:02:18,340 --> 00:02:20,120 but what about the id? 37 00:02:20,470 --> 00:02:26,490 The id is a special field in your data, by default it's always included, 38 00:02:26,530 --> 00:02:29,680 so the default in general is everything is included 39 00:02:29,680 --> 00:02:35,650 but even if you use projection as we do it with the second argument, the id is always included too. 40 00:02:35,920 --> 00:02:42,460 If you use projection like this normally, only what you add here, so name in this case is included but 41 00:02:42,460 --> 00:02:49,030 the id is also always included, you have to explicitly exclude it if you don't want to add it. 42 00:02:49,120 --> 00:02:54,890 Now you exclude something by simply specifying its name and then using 0 instead of 1, 43 00:02:54,940 --> 00:03:00,370 we could do the same for age but this is not required because the default for everything but the id 44 00:03:00,370 --> 00:03:06,730 is that it's not included if you don't add it in your projection configuration but for the ID, we have to 45 00:03:06,730 --> 00:03:07,960 be very clear about it. 46 00:03:08,080 --> 00:03:12,010 And now with that if I hit enter, we get only the objects with the names. 47 00:03:12,010 --> 00:03:14,210 So this is projection doing its job 48 00:03:14,350 --> 00:03:21,970 and the important thing to understand here is that this filtering or this data transformation is happening 49 00:03:21,970 --> 00:03:23,700 on the mongodb server, 50 00:03:23,860 --> 00:03:26,560 so this happens before the data is shipped to you 51 00:03:26,980 --> 00:03:31,030 and this is of course what you want because then you don't get unnecessary data, 52 00:03:31,030 --> 00:03:36,130 you don't impact your bandwidth and therefore you can only work with what you need in this case.