Ok so I'm back to the original data format we had by using find one
and what else did we want to transform?
Well I mentioned that I also want to transform the date of birth field,
I want to keep the birth date but in a separate top level field
and I want to keep the age in a separate top level field.
Now definitely feel free to try this on your own because this is a great practice,
so here's your chance to pause the video, thereafter
we'll do this together.
Were you successful?
Let's transform the birth data together. For this in our existing aggregation pipeline here,
I will add a new field which I want to change,
I'll add it here above location,
you could also add a new project stage if you want it to have multiple project stages to keep this a
bit cleaner,
this is totally up to you.
Now I will add a new birth date field, again adding new fields is no problem.
The birth date field will in general hold the value of dob date, so we could simply say $dob
date, dollar sign to tell mongodb that we refer to a field in the input document.
This is something we could generally do,
however this will of course keep the birth date as a string because it is stored as a string here.
Now thankfully, this string already is in a great format that can easily be converted by mongodb,
so we should convert it. For that,
we can of course use the convert method or the convert operator again.
So on the birth date here, I will instead say convert, the input will be my dob date,
I need to close two curly braces and I want to convert to the date type and you find that type in
the official docs
as I showed you in the last lecture. We can also specify on error and on null
but I'll omit this here, I should be fine, I should have a birth date on every record and I will also now
pull out the age.
Now that will be simple,
the age is a number, I want to keep it as a number,
I will just pull it out of dob.age. So I will simply say age is now a top level field,
I add it a top level field here
and of course I could name it differently,
just as I named it birth date here and not date,
So here I will refer to dob.age and I will keep the type as it is. Now
one important thing, since I got two project stages and I added two new fields in the first one,
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
too and just make sure that they are included in the output of this project stage.
So now with that, let's again move aggregate up and let's move pretty up, let's copy the entire content
here
and execute it and we can see we got a birth date which is now this special ISO date object or type
provided by mongodb and we got our age as a top level field. And now I really transformed my documents
quite a lot already,
I got some key information in every document here and this is looking a really great and really helpful.
Now let's see what else we can do and which other nice pipeline stages we might have.
Well there's one more thing about convert though.