Return a subset of properties of a MongoDB Document (projections, $slice)

In this lesson, we learn that when querying MongoDB collections, we can control which properties are returned by using projections with the findOne method.

We demonstrate how to include or exclude fields by setting them to 1 or 0, respectively. For example, we can choose to include the name and position while excluding the _id field.

We also cover how to access nested properties and project specific elements from arrays using the $slice operator, allowing for more precise data retrieval based on our needs.

Share with a coworker

Transcript

[00:00] When querying Mongo collections, by default, what Mongo is going to return is all the properties that exist on a certain resulting document. However, it's not always the case that we want all the properties to be returned. Now, we're going to use find one, and the first parameter is going to be the query that would specify the criteria that needs to be, yep, satisfied by the document. So here we are just leaving it blank because that's not relevant and we're going to focus on the second parameter that is called the projection. So by default we have all the properties that do exist over here and here the convention is that if we specify 1 then a property is going to be included, if we specify 0 then it's going to be excluded.

[00:52] So if we pass the underscore ID with 0 then ID is going to be avoided. On the other hand, if we specify that from all the things, we just want the name, then we are going to get only what we specified explicitly and by default, all the rest is going to be removed. Now, this is the default that is somehow overwritten over here. So let's say that what we want is the name to be included, there is also the position that we would like to include, there is the ID that we want to exclude because we can overwrite it And to make things even more interesting we can walk into the nested properties as well. So let's specify that we want personal info and on personal info we have the email.

[01:44] So let's put the email over here and let's say that we want it. However, since this is a nested property, we just need to wrap it with the string. So let's now take a look at how could we project an array here. So We're going to reuse the employees find one and let's just break this into multiple lines and we're going to first include the skills array by passing one but that means that we're going to include the entire array and what if we wanted just to provide a small part of it. So again if we want to make something more specific then probably there are some operators that would allow us to do this and slice is the one that does the job.

[02:31] So since this is an operator, we need to prepend it with a dollar. And the first option is that we would basically take the first two elements, which should be JavaScript and React. And that is, Or we could provide a slice that starts with a position and ends with one. So let's also replace this with array, the start position and the end position. So here is the Node.js and MongoDB, which is position two, since this is started from the 0 1 2 & 3