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:
Let's say we want to find a comic book where the writer's name is "Stan Lee". Here's how you do it:
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:
Here are some examples of querying arrays in MongoDB:
`$in` in Action
