Video Transcript (English)
Hi! In this video, we’ll learn how to return selected fields from a query.
By default, queries in MongoDB return all fields in matching documents.
Sometimes it’s not necessary to return all fields because the application will only use data from a subset of fields.
In this case, we can limit the amount of data sent from MongoDB, and improve the performance of the application, by selecting which fields are returned.
Projection is what we call this process of selecting the fields returned in the result set.
Let's see how to select fields to return from a query!
We’re working with a database called `training` and querying its `inspections` collection.
This collection contains data on business inspections.
We run a `findOne()` query to see a sample document.
We see this document has all sorts of data about a business inspection, including the business name, inspection date, and the result.
Say we want to return the data on only the restaurant inspections. For each document we just want to return the business name, result, and `_id` value.
We start by writing a query to return only the restaurant inspections.
To specify the fields to include, we add a projection document as our second parameter.
The projection document consists of field-value pairs. We add the two fields we’re interested in, and set their values to 1 to include them.
The `_id` field is included by default, so we don’t need to add it to the projection.
We run the query and see that only the fields we selected are returned.
If we don’t want to return the `_id` field, we can exclude it from results by adding it to the projection and setting its value to 0.
We run the query again. Now only the business name and result fields are returned.
It’s important to note that inclusion and exclusion statements can’t be combined in most projections. The `_id` field is an exception.
In general we can either choose fields to include or exclude in results, but not both - except in the case of `_id`.
We’ve seen how to select fields to return from a query.
And we’ve seen that while the `_id` field is included by default, we can suppress it from results.
Now let’s see how we choose fields to exclude, so that only fields that aren’t explicitly excluded are returned.
Say we want to return data on all inspections where the result was “Pass” or “Warning”. We want to return all fields except the date and the zip code.
First we select documents where the result was either “Pass” or “Warning.”
Then we add the projection, specifying the two fields we want to exclude and setting their values to 0.
Note that we use dot notation when referring to embedded fields, like “address.zip”.
We run the query. All fields except `date` and `address.zip` are returned - great!
Let’s recap a few takeaways from this video. We saw how to return selected fields from a query.
We learned that projection is what we call this process of selecting the fields returned in the result set.
Projections are useful because they limit the amount of data returned from the database, which can improve application performance.
We saw how to select fields to return by adding a projection document as our second parameter in calls to `find()`.
To select the fields to include, we set their values to 1.
To exclude fields, we set their values to 0.
Remember that inclusion & exclusion statements can't be combined in projections.
The `_id` field is an exception.
Congratulations, you’re ready to start using projection in your projects!