So you learned a lot about projection and that there are many many many operators you can use for that
in the last lectures, which is super important because that is one of the core things
the aggregation pipeline is all about.
Now let's project a bit more because this really is an important step of almost any pipeline you're
going to build
and for that, let's first of all have a look at our input data
one more time.
Thus far, we basically only included the gender and our transformed name here,
now what else could we include?
Well for example, the location, we could already prepare the location so that we can later work with it,
so what we could do is we could basically turn the location into a geo json object where we have a
type which will be a point because we have two coordinates and then we have coordinates but that should
then be an array of two numbers instead of an object of latitude and longitude which also are strings
and not numbers.
So this will again be quite a complex transformation but one which makes sense here and which we will
need.
I also want to include my email in the output and I want to include my date of birth data but I also
want to transform this to basically flatten it to not have a nested document but simply have a birth
date which is this and then an age field as a top level field.
Now definitely feel free to pause and do this on your own but we will need a couple of operators we
haven't touched on yet,
so also feel free to follow along with me depending on whether you feel like diving into the official
docs or not.
So let's start with the location,
now what I will do to keep this a bit more manageable, I will add an extra project stage and this already
also shows you something else which is important,
you can have the same stage multiple times and that's not that uncommon
to be honest, often you also do some matching, sorting, grouping and then you project and then maybe you
sort
again, so often you have some in-between stages, here I split it across two stages to make it easier to read.
So I will
exclude my ID here already and therefore, I don't have to do it here because remember, this stage will
receive the output of this stage,
so if I exclude the ID here, it will not be available in the next stage,
I should put this between curly braces though. Now I will add the name here,
that should be included because the name is something I need in the next stage,
so we have to make sure that we pass this embedded document
and then I want to work on the location and so on.
Now first of all, I'll pass on the e-mail because that will also just be in the next stage without any
changes and therefore in the next stage, I'll also keep that email and well output it as a result of
this
stage too
and now let's work on the location.
Now the location at the moment is an embedded document with a lot of data that I'm currently not interested
in for this operation and where I also well have it in the wrong format,
I want to have a type and then coordinates which should be an array. So location should be a document,
this is the case
and in that document, I want to have a type field which will be point. And that is something we can do,
we can add hardcoded values like this.
So now I added this,
now I also need to add my coordinates, the coordinates right now are a nested document like this,
I need an array of coordinates though. And this is relatively simple to achieve,
I simply set coordinates to an array
and now the first value here will be dollar sign
and now I need to access location coordinates
longitude because remember, for a geo json object, the first value has to be a longitude.
So I access location coordinates longitude here,
so this field is getting accessed here,
the dollar sign is important to tell mongodb that does this not a hardcoded string value but
instead, the value stored in this path here in our document
and the second element of the coordinates array is almost the same
but of course here, I refer to latitude,
so now this is my project phase as it is currently written.
So now location should be such a geo json object,
let's give this a try. For this,
let's also include location which is this location because we get the output of the last stage as an input
to this stage,
so we will have that location key which is the result of this location transformation and let's include
that in our new output.
Now again, aggregate has to go on this line and pretty has to go on this line
and now let's copy that entire stage here
and let's run it. And what we can see is that for every person, we indeed have the full name,
we have the email and location indeed is a point or is a geo json object with type point and coordinates.
Now one thing you might also see is that
this however is a string and not a number which it should be,
now to change that, we have to convert this string. Converting data is something you often have to do,
we'll have to do it later again for the birth date
but now let's do it for our coordinates here.
Of course mongodb also has a convert operator we can use,
so we want to convert our coordinates here.
So the value I store as longitude will be the result of an expression using the $convert
operator, the
$convert operator takes a document where you configure it, so where you configure its inputs and so on.
Now convert needs a couple of fields,
the first field is input and there you simply define which value should be converted,
this of course is the value which is stored in a longitude field in our case.
The second field
and this is the last field that you need to pass is the to field, the to field defines the type you want to
convert to.
Now which types are available?
This is of course also something you find in the official docs of the convert method, there
you find that the to field takes these string identifiers to convert it to a double or to a decimal
which is simply a constant with higher precision than a normal double and so on.
You also can define on error and on null values,
so default values that should be returned in case the transformation fails.
So here I want to convert this to a double which is a floating point value and on error will be 0,
on null will also be 0 or a 0.0,
so we store or we return just 0
if we fail to convert this. Now I can take this entire step here and replace my latitude step with it,
of course replacing longitude with latitude down there again.
So now these coordinates should be transformed,
so let's now bring aggregate and pretty back into line
and let's copy the entire stage or the entire pipeline and execute it.
And now we see that the coordinates indeed are numbers,
they are no longer wrapped in quotation marks.