Lesson 3
Querying Embedded Fields and Arrays
Introduction

Welcome! In this lesson, we’ll explore how to query nested fields and arrays in MongoDB. Additionally, we will cover two comparison operators, $in and $nin, that were not discussed in the previous lesson. Let's get started!

Querying Nested Fields

When documents contain nested fields, like sub-documents, querying can be achieved using dot notation. For example, consider a document in our comic_book_store_db database:

JSON
1{ 2 "title": "The Amazing Spider-Man", 3 "issue_number": 1, 4 "published_year": 1963, 5 "writer": { 6 "name": "Stan Lee", 7 "nationality": "American" 8 } 9 // Other fields 10}

Let's say we want to find a comic book where the writer's name is "Stan Lee". Here's how you do it:

JavaScript
1use comic_book_store_db 2 3db.comic_books.findOne( 4 { "writer.name": "Stan Lee" }, 5 { title: 1, writer: 1, _id: 0 } 6)

This query searches within the sub-document writer for the field name. Note that writer.name should be in quotes; otherwise, the . will cause an error in your query.

Querying Arrays

Arrays are common in many documents. Our comic_book_store_db has fields like genres and characters, which are arrays. Here's an example of a document containing arrays:

JSON
1{ 2 "title": "The Amazing Spider-Man", 3 "issue_number": 1, 4 "published_year": 1963, 5 "genres": ["Action", "Adventure", "Superhero"] 6 "characters": [ 7 { "name": "Spider-Man", "real_name": "Peter Parker" }, 8 { "name": "Green Goblin", "real_name": "Norman Osborn" } 9 ] 10 // Other fields 11}

Here are some examples of querying arrays in MongoDB:

JavaScript
1use comic_book_store_db 2 3// Locate a comic book where the genre includes "Adventure" 4db.comic_books.findOne( 5 { genres: "Adventure" }, // Note that we're not using brackets here 6 { title: 1, genres: 1, _id: 0 } 7) 8 9// Find a comic book which has an exact array of genres, in the specified order 10db.comic_books.findOne( 11 { genres: ["Action", "Adventure", "Superhero"] }, // Note the brackets! 12 { title: 1, genres: 1, _id: 0 } 13) 14 15// Find a comic book where a character named "Spider-Man" appears 16db.comic_books.findOne( 17 { "characters.name": "Spider-Man" }, 18 { title: 1, "characters.name": 1, _id: 0 } 19)
`$in` in Action

The $in operator is used to match any value within an array of specific values. If we want a comic book that belongs to either 'Action' or 'Adventure' genres:

JavaScript
1use comic_book_store_db 2 3db.comic_books.findOne( 4 { genres: { $in: ["Action", "Adventure"] } }, 5 { title: 1, genres: 1, _id: 0 } 6)
Using `$nin`

Conversely, the $nin operator matches any value that does not exist in the specified array. To find a comic book where the writer's nationality is not "American" or "British":

JavaScript
1use comic_book_store_db 2 3db.comic_books.findOne( 4 { "writer.nationality": { $nin: ["American", "British"] } }, 5 { title: 1, writer: 1, _id: 0 } 6)
Summary

In this lesson, we've explored querying nested fields and arrays within MongoDB documents. We used dot notation to dig into sub-documents and arrays, and powerful operators like $in and $nin to refine our queries. Practice these techniques to enhance your MongoDB querying skills and easily navigate complex document structures. Keep experimenting!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.