We already covered a lot about how to retrieve data, how to store data, update and delete it
and we're nearing the end of this module
but before we conclude the module, there are two other important things you gotta know
and the first one is projection.
Now what is projection?
Imagine you have this kind of data in your database, some information about a person,
in this case me with name, age, job and that ID.
Now imagine in your application,
you don't need all this data,
maybe you just need the name and age for a given page on your website.
Of course you could fetch all the data and filter it in your application, in any programming language
you can of course manipulate the data you're getting back but you would still transfer it over to the
wire,
so you would kind of impact your bandwidth, you would fetch unnecessary data and you want to prevent this,
so it would be better to filter this out on the mongodb server
and this is exactly what projection can help you with. Projection is super easy to use,
let me show it to you.
We've got our passengers and on the passengers, let's do a quick find again, here are our passengers and
now let's say I'm only interested in the names of the passengers.
Now you can do projection by using find,
now you need to pass a first argument because I actually want to pass a second argument and that first
argument now is an empty document because I want to find all passengers and I don't want to pass a filter
and the second argument now allows us to project. And here, the projection is set up by passing another
document,
so another pair of curly braces
and here you simply specify which key value pairs you want to get.
For that, you simply name the field you want to get,
so let's say in our passengers, we have a name and we want to get that name,
so we type name here and then as a value, simply a one,
this means include it in the data you're returning to me. If we now output that, we get all our fields
with just a name and well still the ID,
so the age is missing and the age is of course not missing in a database but only in the data we retrieved
from it,
so in the data we're showing here.
So this is projection doing its job
but what about the id?
The id is a special field in your data, by default it's always included,
so the default in general is everything is included
but even if you use projection as we do it with the second argument, the id is always included too.
If you use projection like this normally, only what you add here, so name in this case is included but
the id is also always included, you have to explicitly exclude it if you don't want to add it.
Now you exclude something by simply specifying its name and then using 0 instead of 1,
we could do the same for age but this is not required because the default for everything but the id
is that it's not included if you don't add it in your projection configuration but for the ID, we have to
be very clear about it.
And now with that if I hit enter, we get only the objects with the names.
So this is projection doing its job
and the important thing to understand here is that this filtering or this data transformation is happening
on the mongodb server,
so this happens before the data is shipped to you
and this is of course what you want because then you don't get unnecessary data,
you don't impact your bandwidth and therefore you can only work with what you need in this case.