How to Include Nested Fields in a find Query
In production applications that utilize Mongo's database a very common pattern that you'll see is that your documents start to get pretty big and you start to have a number of nested collections. It's one of the top reasons why you actually use a document based system like Mongo because you have the flexibility and then you also have the ability to nest as many items as you want.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Just like we have right here

large

Now, I've extended our blank document here and in addition to just having a name for authors we now also have if they're active or not. I also deleted all the blank instances in our database so that we can just be working with a single one. And now if I paste this in you can see that we got one inserted.

WriteResult{"nInsterted" : 1 })

Now, I want the ability to query our document here and I do not want to list if the author is active or not. Imagine a scenario where you're querying it to show an author page on the Website you don't care about if the author is active for that page you simply need to know their name. What we can do is we can actually go in and select the nested elements that we want

db.books.find(

)

As usual, we're going to set up two different objects. The first is going to be our query which would be Blink. And then we're going to set up our projections.

db.books.find(
  {
    name: "Blink"
  },
)

Now our projections are going to be pretty standard so we're going to have a name of one because I want the name back and then a publish date of 1 because I'd like that date and the next part is going to look a little bit different. We can't use our same syntax as we're using right here where we write it without the strings. We do need to embed this in strings. And what we can say is authors which is a name of our collection and then name and so we have authors which are an array and then inside of it there are all of those various elements we have all those objects and so when we do authors dot name what Mongo's going to do it's going to go into that array and then it's all going to go through each one of those objects each one of those authors objects and then it's going to bring back the name attribute only and we can give that a 1 and then we'll also add the pretty function at the end. And now let's run this. If I run this now you can see that it only returns the authors and their respective names. It doesn't bring back if they're active or not.

large

Now just to prove that that is the way it's really working. If I delete most of this and just say authors and run that you can see that's where it brings active true on both of those.

large

But the biggest key here to remember is you have the ability with those embedded nested objects to be able to go through them by first calling the name of whatever the key is here and then follow that up by whatever the key is inside of all the nested objects. This kind of gets to the heart on why Mongo was designed the way that it was is because it has that key-value kind of set up. It gives us the ability to treat it just like a regular set of objects the same way we would in javascript or any language like that where we have the ability to call collections and then go inside of those and then call the respective keys so that we can retrieve the values.

Code

db.books.insert({
    "name": "Blink",
    "publishedDate": new Date(),
    "authors": [
        { "name": "Malcolm Gladwell", "active": "true" },
        { "name": "Ghost Writer", "active": "true" }
    ]
});
db.books.find(
  {
    name: "Blink"
  },
  {
    name: 1,
    publishedDate: 1,
    "authors.name": 1
  }
).pretty()