1 00:00:02,280 --> 00:00:07,790 Ok so I'm back to the original data format we had by using find one 2 00:00:07,930 --> 00:00:10,750 and what else did we want to transform? 3 00:00:11,020 --> 00:00:15,540 Well I mentioned that I also want to transform the date of birth field, 4 00:00:15,610 --> 00:00:18,850 I want to keep the birth date but in a separate top level field 5 00:00:19,090 --> 00:00:23,130 and I want to keep the age in a separate top level field. 6 00:00:23,230 --> 00:00:28,570 Now definitely feel free to try this on your own because this is a great practice, 7 00:00:28,570 --> 00:00:31,500 so here's your chance to pause the video, thereafter 8 00:00:31,540 --> 00:00:32,890 we'll do this together. 9 00:00:35,300 --> 00:00:36,400 Were you successful? 10 00:00:36,530 --> 00:00:44,510 Let's transform the birth data together. For this in our existing aggregation pipeline here, 11 00:00:45,020 --> 00:00:47,660 I will add a new field which I want to change, 12 00:00:47,660 --> 00:00:49,280 I'll add it here above location, 13 00:00:49,280 --> 00:00:55,220 you could also add a new project stage if you want it to have multiple project stages to keep this a 14 00:00:55,220 --> 00:00:55,970 bit cleaner, 15 00:00:55,970 --> 00:00:57,620 this is totally up to you. 16 00:00:57,740 --> 00:01:03,370 Now I will add a new birth date field, again adding new fields is no problem. 17 00:01:03,440 --> 00:01:13,260 The birth date field will in general hold the value of dob date, so we could simply say $dob 18 00:01:13,310 --> 00:01:20,000 date, dollar sign to tell mongodb that we refer to a field in the input document. 19 00:01:20,000 --> 00:01:22,970 This is something we could generally do, 20 00:01:22,970 --> 00:01:30,910 however this will of course keep the birth date as a string because it is stored as a string here. 21 00:01:31,010 --> 00:01:38,060 Now thankfully, this string already is in a great format that can easily be converted by mongodb, 22 00:01:38,060 --> 00:01:41,350 so we should convert it. For that, 23 00:01:41,350 --> 00:01:46,180 we can of course use the convert method or the convert operator again. 24 00:01:46,600 --> 00:01:55,380 So on the birth date here, I will instead say convert, the input will be my dob date, 25 00:01:55,410 --> 00:02:02,940 I need to close two curly braces and I want to convert to the date type and you find that type in 26 00:02:02,940 --> 00:02:03,970 the official docs 27 00:02:03,970 --> 00:02:09,050 as I showed you in the last lecture. We can also specify on error and on null 28 00:02:09,190 --> 00:02:15,630 but I'll omit this here, I should be fine, I should have a birth date on every record and I will also now 29 00:02:15,670 --> 00:02:17,260 pull out the age. 30 00:02:17,300 --> 00:02:18,900 Now that will be simple, 31 00:02:18,910 --> 00:02:21,820 the age is a number, I want to keep it as a number, 32 00:02:21,820 --> 00:02:29,860 I will just pull it out of dob.age. So I will simply say age is now a top level field, 33 00:02:29,860 --> 00:02:31,890 I add it a top level field here 34 00:02:33,100 --> 00:02:35,230 and of course I could name it differently, 35 00:02:35,230 --> 00:02:38,620 just as I named it birth date here and not date, 36 00:02:38,620 --> 00:02:43,830 So here I will refer to dob.age and I will keep the type as it is. Now 37 00:02:43,870 --> 00:02:50,040 one important thing, since I got two project stages and I added two new fields in the first one, 38 00:02:50,140 --> 00:02:57,930 I need to go to the second one and take these new fields with me, so I need to add the two new fields here 39 00:02:57,940 --> 00:03:02,210 too and just make sure that they are included in the output of this project stage. 40 00:03:03,950 --> 00:03:14,100 So now with that, let's again move aggregate up and let's move pretty up, let's copy the entire content 41 00:03:14,100 --> 00:03:14,660 here 42 00:03:16,290 --> 00:03:23,820 and execute it and we can see we got a birth date which is now this special ISO date object or type 43 00:03:23,820 --> 00:03:30,870 provided by mongodb and we got our age as a top level field. And now I really transformed my documents 44 00:03:30,870 --> 00:03:32,570 quite a lot already, 45 00:03:32,580 --> 00:03:39,060 I got some key information in every document here and this is looking a really great and really helpful. 46 00:03:39,060 --> 00:03:43,620 Now let's see what else we can do and which other nice pipeline stages we might have. 47 00:03:43,740 --> 00:03:45,810 Well there's one more thing about convert though.