1 00:00:02,310 --> 00:00:04,200 Now we're almost done with the module, 2 00:00:04,230 --> 00:00:09,500 we learned a lot about querying operators and also about cursors, 3 00:00:09,510 --> 00:00:17,520 now let's talk about how we can shape the data which we do get back into the form we needed in. Because 4 00:00:17,520 --> 00:00:25,270 in the example of our movies, we have all that data for every document we return, that might simply be too 5 00:00:25,270 --> 00:00:26,210 much data, 6 00:00:26,230 --> 00:00:30,390 not only is it a lot of redundant data that we transfer over the wire 7 00:00:30,430 --> 00:00:33,940 if we don't need it, it also makes it harder for it to work with the data 8 00:00:33,940 --> 00:00:40,990 if we have to manually parse all that. Now with projection, we can control which data is returned. 9 00:00:41,050 --> 00:00:51,600 Let's say we are only interested in the name, the genres, the runtime and the rating and all the rest does 10 00:00:51,600 --> 00:00:58,410 not matter to us. If that is the case, I can write a query here without any criteria, 11 00:00:58,410 --> 00:01:01,800 let's say we fetch all documents in a pretty way 12 00:01:02,160 --> 00:01:06,560 but now I actually need to pass a second argument to find. 13 00:01:06,660 --> 00:01:12,330 Now for that, I also need to pass a first one and if I don't want to specify any criteria, any filter, 14 00:01:12,660 --> 00:01:16,350 I just add empty curly braces like this. 15 00:01:16,350 --> 00:01:21,730 The second argument to find then allows us to configure how values are projected, 16 00:01:21,750 --> 00:01:25,430 so how we actually extract the data fields 17 00:01:25,770 --> 00:01:35,300 and here we have a very simple syntax. If we want to get the name and the genres and the runtime and the 18 00:01:35,300 --> 00:01:36,170 rating, 19 00:01:36,170 --> 00:01:45,980 then we simply add name and add a one, that means the name field is something I want to include, the genres, 20 00:01:46,120 --> 00:01:50,170 that's also something I want to include as is the runtime, 21 00:01:50,170 --> 00:01:54,050 now last but not least, I also want to include the rating. 22 00:01:54,100 --> 00:02:02,890 Now all fields that I don't mention here with a one or that I explicitly add with a zero, like let's 23 00:02:02,890 --> 00:02:07,990 say image are not included but I don't need to add image zero here, 24 00:02:07,990 --> 00:02:09,640 it will be excluded by default. 25 00:02:09,880 --> 00:02:12,950 So if I hit enter, now I get a reduced version, 26 00:02:13,180 --> 00:02:20,350 you'll see that the ID is always included though and that is one exception, the ID is always included 27 00:02:20,470 --> 00:02:26,230 even if you don't specify it with a one. If you want to exclude the ID, that is possible but you have 28 00:02:26,230 --> 00:02:28,860 to explicitly set _ID to zero, 29 00:02:29,080 --> 00:02:33,190 that is not required for any other field but for the ID, it is 30 00:02:33,190 --> 00:02:36,270 and now we have our data without the ID. 31 00:02:36,310 --> 00:02:41,660 So now we return just the fields for every document that we are interested in 32 00:02:41,740 --> 00:02:44,350 which is of course very very helpful. 33 00:02:46,370 --> 00:02:55,000 You can also project on embedded documents, like for rating, you could say for the rating, 34 00:02:55,000 --> 00:03:03,870 I'm only interested in the average field and then you would simply use the path notation, rating.average 35 00:03:03,990 --> 00:03:05,580 and set this to one, 36 00:03:05,580 --> 00:03:11,840 however this will result in the same result because rating was an embedded document with only the average. 37 00:03:12,120 --> 00:03:17,000 So we can use a different example of course, in our data structure 38 00:03:17,010 --> 00:03:27,230 if I go up a bit, we see that we also have the schedule and let's say we're only interested in the time. 39 00:03:27,830 --> 00:03:32,200 Well schedule is an embedded document so we can just get the time 40 00:03:35,080 --> 00:03:40,290 by adding schedule.time and set this to one 41 00:03:40,460 --> 00:03:46,550 and now we will notice, the time is included but schedule also had an array of the days and that is 42 00:03:46,550 --> 00:03:47,900 now not included, 43 00:03:47,900 --> 00:03:51,450 so projection can also be used on embedded documents. 44 00:03:51,560 --> 00:03:57,650 Now projection can also work with arrays and help us a bit with array data that we include, 45 00:03:57,650 --> 00:03:59,660 now let me show you how that works.