Now we're almost done with the module,
we learned a lot about querying operators and also about cursors,
now let's talk about how we can shape the data which we do get back into the form we needed in. Because
in the example of our movies, we have all that data for every document we return, that might simply be too
much data,
not only is it a lot of redundant data that we transfer over the wire
if we don't need it, it also makes it harder for it to work with the data
if we have to manually parse all that. Now with projection, we can control which data is returned.
Let's say we are only interested in the name, the genres, the runtime and the rating and all the rest does
not matter to us. If that is the case, I can write a query here without any criteria,
let's say we fetch all documents in a pretty way
but now I actually need to pass a second argument to find.
Now for that, I also need to pass a first one and if I don't want to specify any criteria, any filter,
I just add empty curly braces like this.
The second argument to find then allows us to configure how values are projected,
so how we actually extract the data fields
and here we have a very simple syntax. If we want to get the name and the genres and the runtime and the
rating,
then we simply add name and add a one, that means the name field is something I want to include, the genres,
that's also something I want to include as is the runtime,
now last but not least, I also want to include the rating.
Now all fields that I don't mention here with a one or that I explicitly add with a zero, like let's
say image are not included but I don't need to add image zero here,
it will be excluded by default.
So if I hit enter, now I get a reduced version,
you'll see that the ID is always included though and that is one exception, the ID is always included
even if you don't specify it with a one. If you want to exclude the ID, that is possible but you have
to explicitly set _ID to zero,
that is not required for any other field but for the ID, it is
and now we have our data without the ID.
So now we return just the fields for every document that we are interested in
which is of course very very helpful.
You can also project on embedded documents, like for rating, you could say for the rating,
I'm only interested in the average field and then you would simply use the path notation, rating.average
and set this to one,
however this will result in the same result because rating was an embedded document with only the average.
So we can use a different example of course, in our data structure
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.
Well schedule is an embedded document so we can just get the time
by adding schedule.time and set this to one
and now we will notice, the time is included but schedule also had an array of the days and that is
now not included,
so projection can also be used on embedded documents.
Now projection can also work with arrays and help us a bit with array data that we include,
now let me show you how that works.