Fields Inside Fields: Dot Notation
Dot notation names a field inside an embedded document, such as "nutrition.calories" in quotes, so a filter can test that one field instead of matching the whole embedded document.
Apprentice · MongoDB · Updated
After this lesson you can
- Find documents by a field inside an embedded document with dot notation, written in quotes
- Explain why matching a whole embedded document is fragile, and use dot notation instead
Key takeaways
- A document can hold another document, such as a recipe's nutrition with its calories and protein inside it.
- Dot notation names a field inside a field, such as "nutrition.calories", read aloud as "nutrition dot calories".
- A field name with a dot in it goes in quotes, or mongosh stops with a SyntaxError.
- Dot notation works with every operator, such as "nutrition.calories" with $lt 200.
- Matching a whole embedded document needs every field, in the same order, so it breaks easily. Test one field with dot notation instead.
A store's search filters by a size that lives inside a product's details, a delivery app finds orders by the city inside an address, and a recipe site lists the meals under 200 calories, where the calories sit inside a recipe's nutrition. Whenever a field lives inside another field, you need a way to point at it, and that's what this lesson gives you.
The examples use Sunday Kitchen's 40 recipes from Adding Many Documents with insertMany, with two new fields on every card. To follow along, open your mongodb-course folder in VS Code, open its terminal, type mongosh, then use sunday_kitchen.
Sunday Kitchen's cards get nutrition and ingredients
Sunday Kitchen's cooks have filled in the back of every card: the calories and protein in one serving, and the ingredients. In VS Code, make a new file in your mongodb-course folder named sunday-kitchen-full-recipes.js, and copy this into it. It holds the same 40 recipes as before, each with two new fields at the end:
// Sunday Kitchen's 40 recipes, with their nutrition and ingredients. Load them in mongosh with: load("sunday-kitchen-full-recipes.js")
// drop() removes the recipes collection first, so running this file twice is safe.
db.recipes.drop();
db.recipes.insertMany([
{ _id: 1, name: "Saturday Pancakes", cuisine: "American", meal: "breakfast", minutes: 25, servings: 4, vegetarian: true, rating: 4.6, tags: ["sweet", "kid-friendly"], nutrition: { calories: 420, protein: 11 }, ingredients: [{ name: "flour", amount: 1.5, unit: "cups" }, { name: "milk", amount: 1.25, unit: "cups" }, { name: "eggs", amount: 2, unit: "whole" }, { name: "butter", amount: 3, unit: "tbsp" }] },
{ _id: 2, name: "Weeknight Chili", cuisine: "American", meal: "dinner", minutes: 45, servings: 6, vegetarian: false, rating: 4.4, tags: ["spicy", "make-ahead"], nutrition: { calories: 480, protein: 32 }, ingredients: [{ name: "ground beef", amount: 1, unit: "lb" }, { name: "kidney beans", amount: 2, unit: "cans" }, { name: "onion", amount: 1, unit: "whole" }, { name: "garlic", amount: 3, unit: "cloves" }] },
{ _id: 3, name: "Lemon Bars", cuisine: "American", meal: "dessert", minutes: 50, servings: 16, vegetarian: true, rating: 4.8, tags: ["sweet", "bake-sale"], nutrition: { calories: 210, protein: 3 }, ingredients: [{ name: "flour", amount: 2, unit: "cups" }, { name: "sugar", amount: 1.5, unit: "cups" }, { name: "eggs", amount: 4, unit: "whole" }, { name: "lemon", amount: 3, unit: "whole" }] },
{ _id: 4, name: "Chickpea Curry", cuisine: "Indian", meal: "dinner", minutes: 35, servings: 4, vegetarian: true, rating: 4.7, tags: ["vegan", "spicy"], nutrition: { calories: 390, protein: 14 }, ingredients: [{ name: "chickpeas", amount: 2, unit: "cans" }, { name: "coconut milk", amount: 1, unit: "cans" }, { name: "onion", amount: 1, unit: "whole" }, { name: "garlic", amount: 4, unit: "cloves" }] },
{ _id: 5, name: "Pad Thai", cuisine: "Thai", meal: "dinner", minutes: 30, servings: 2, vegetarian: false, rating: 4.5, tags: ["noodles"], nutrition: { calories: 560, protein: 28 }, ingredients: [{ name: "rice noodles", amount: 8, unit: "oz" }, { name: "shrimp", amount: 0.5, unit: "lb" }, { name: "eggs", amount: 2, unit: "whole" }, { name: "lime", amount: 1, unit: "whole" }] },
{ _id: 6, name: "Margherita Pizza", cuisine: "Italian", meal: "dinner", minutes: 40, servings: 4, vegetarian: true, rating: 4.3, tags: ["kid-friendly"], nutrition: { calories: 610, protein: 24 }, ingredients: [{ name: "flour", amount: 3, unit: "cups" }, { name: "tomatoes", amount: 1, unit: "cans" }, { name: "mozzarella", amount: 8, unit: "oz" }, { name: "basil", amount: 0.5, unit: "cups" }] },
{ _id: 7, name: "Black Bean Tacos", cuisine: "Mexican", meal: "dinner", minutes: 20, servings: 4, vegetarian: true, rating: 4.2, tags: ["vegan", "kid-friendly"], nutrition: { calories: 380, protein: 15 }, ingredients: [{ name: "black beans", amount: 2, unit: "cans" }, { name: "tortillas", amount: 8, unit: "whole" }, { name: "avocado", amount: 1, unit: "whole" }, { name: "lime", amount: 1, unit: "whole" }] },
{ _id: 8, name: "Miso Soup", cuisine: "Japanese", meal: "lunch", minutes: 15, servings: 2, vegetarian: true, rating: 3.9, tags: ["soup"], nutrition: { calories: 90, protein: 7 }, ingredients: [{ name: "miso paste", amount: 3, unit: "tbsp" }, { name: "tofu", amount: 7, unit: "oz" }, { name: "green onions", amount: 2, unit: "whole" }] },
{ _id: 9, name: "Greek Salad", cuisine: "Greek", meal: "lunch", minutes: 15, servings: 4, vegetarian: true, rating: 4.1, tags: ["no-cook"], nutrition: { calories: 260, protein: 7 }, ingredients: [{ name: "cucumber", amount: 1, unit: "whole" }, { name: "tomatoes", amount: 3, unit: "whole" }, { name: "feta", amount: 4, unit: "oz" }, { name: "lemon", amount: 1, unit: "whole" }] },
{ _id: 10, name: "Chicken Tikka Masala", cuisine: "Indian", meal: "dinner", minutes: 60, servings: 4, vegetarian: false, rating: 4.9, tags: ["spicy"], nutrition: { calories: 520, protein: 41 }, ingredients: [{ name: "chicken", amount: 2, unit: "lb" }, { name: "yogurt", amount: 1, unit: "cups" }, { name: "tomatoes", amount: 1, unit: "cans" }, { name: "garlic", amount: 4, unit: "cloves" }] },
{ _id: 11, name: "Banana Bread", cuisine: "American", meal: "snack", minutes: 70, servings: 10, vegetarian: true, rating: 4.5, tags: ["sweet", "bake-sale"], nutrition: { calories: 290, protein: 4 }, ingredients: [{ name: "flour", amount: 1.75, unit: "cups" }, { name: "bananas", amount: 3, unit: "whole" }, { name: "sugar", amount: 0.75, unit: "cups" }, { name: "eggs", amount: 2, unit: "whole" }] },
{ _id: 12, name: "Spaghetti Carbonara", cuisine: "Italian", meal: "dinner", minutes: 25, servings: 4, vegetarian: false, rating: 4.6, tags: ["pasta"], nutrition: { calories: 640, protein: 30 }, ingredients: [{ name: "spaghetti", amount: 1, unit: "lb" }, { name: "eggs", amount: 4, unit: "whole" }, { name: "parmesan", amount: 1, unit: "cups" }, { name: "garlic", amount: 2, unit: "cloves" }] },
{ _id: 13, name: "Veggie Stir-Fry", cuisine: "Chinese", meal: "dinner", minutes: 20, servings: 3, vegetarian: true, tags: ["vegan"], nutrition: { calories: 310, protein: 9 }, ingredients: [{ name: "broccoli", amount: 2, unit: "cups" }, { name: "carrots", amount: 2, unit: "whole" }, { name: "soy sauce", amount: 3, unit: "tbsp" }, { name: "garlic", amount: 3, unit: "cloves" }] },
{ _id: 14, name: "Beef Pho", cuisine: "Vietnamese", meal: "dinner", minutes: 180, servings: 6, vegetarian: false, rating: 4.8, tags: ["soup", "noodles"], nutrition: { calories: 450, protein: 35 }, ingredients: [{ name: "rice noodles", amount: 1, unit: "lb" }, { name: "beef", amount: 0.5, unit: "lb" }, { name: "onion", amount: 1, unit: "whole" }, { name: "star anise", amount: 4, unit: "whole" }] },
{ _id: 15, name: "Shakshuka", cuisine: "Middle Eastern", meal: "breakfast", minutes: 30, servings: 4, vegetarian: true, rating: 4.4, tags: ["spicy"], nutrition: { calories: 240, protein: 13 }, ingredients: [{ name: "eggs", amount: 6, unit: "whole" }, { name: "tomatoes", amount: 2, unit: "cans" }, { name: "onion", amount: 1, unit: "whole" }, { name: "garlic", amount: 3, unit: "cloves" }] },
{ _id: 16, name: "French Toast", cuisine: "French", meal: "breakfast", minutes: 15, servings: 2, vegetarian: true, rating: 4.0, tags: ["sweet"], nutrition: { calories: 350, protein: 13 }, ingredients: [{ name: "bread", amount: 4, unit: "slices" }, { name: "eggs", amount: 2, unit: "whole" }, { name: "milk", amount: 0.5, unit: "cups" }] },
{ _id: 17, name: "Tiramisu", cuisine: "Italian", meal: "dessert", minutes: 30, servings: 8, vegetarian: true, rating: 4.7, tags: ["sweet", "make-ahead"], nutrition: { calories: 410, protein: 8 }, ingredients: [{ name: "ladyfingers", amount: 24, unit: "whole" }, { name: "mascarpone", amount: 16, unit: "oz" }, { name: "eggs", amount: 4, unit: "whole" }, { name: "coffee", amount: 1, unit: "cups" }] },
{ _id: 18, name: "Chicken Fried Rice", cuisine: "Chinese", meal: "dinner", minutes: 25, servings: 4, vegetarian: false, rating: 4.0, tags: ["leftovers"], nutrition: { calories: 470, protein: 27 }, ingredients: [{ name: "rice", amount: 3, unit: "cups" }, { name: "chicken", amount: 1, unit: "lb" }, { name: "eggs", amount: 2, unit: "whole" }, { name: "peas", amount: 1, unit: "cups" }] },
{ _id: 19, name: "Lentil Soup", cuisine: "Greek", meal: "lunch", minutes: 45, servings: 6, vegetarian: true, rating: null, tags: ["vegan", "soup"], nutrition: { calories: 280, protein: 16 }, ingredients: [{ name: "lentils", amount: 1.5, unit: "cups" }, { name: "carrots", amount: 2, unit: "whole" }, { name: "garlic", amount: 3, unit: "cloves" }, { name: "lemon", amount: 1, unit: "whole" }] },
{ _id: 20, name: "Guacamole", cuisine: "Mexican", meal: "snack", minutes: 10, servings: 4, vegetarian: true, rating: 4.3, tags: ["vegan", "no-cook"], nutrition: { calories: 160, protein: 2 }, ingredients: [{ name: "avocado", amount: 3, unit: "whole" }, { name: "lime", amount: 1, unit: "whole" }, { name: "onion", amount: 0.5, unit: "whole" }] },
{ _id: 21, name: "Palak Paneer", cuisine: "Indian", meal: "dinner", minutes: 40, servings: 4, vegetarian: true, tags: ["gluten-free"], nutrition: { calories: 370, protein: 18 }, ingredients: [{ name: "spinach", amount: 1, unit: "lb" }, { name: "paneer", amount: 8, unit: "oz" }, { name: "cream", amount: 0.25, unit: "cups" }, { name: "garlic", amount: 3, unit: "cloves" }] },
{ _id: 22, name: "Caesar Salad", cuisine: "American", meal: "lunch", minutes: 20, servings: 2, vegetarian: false, rating: 3.8, tags: ["no-cook"], nutrition: { calories: 330, protein: 10 }, ingredients: [{ name: "romaine", amount: 2, unit: "whole" }, { name: "parmesan", amount: 0.5, unit: "cups" }, { name: "lemon", amount: 1, unit: "whole" }, { name: "garlic", amount: 2, unit: "cloves" }] },
{ _id: 23, name: "Chocolate Chip Cookies", cuisine: "American", meal: "dessert", minutes: 30, servings: 24, vegetarian: true, rating: 4.9, tags: ["sweet", "bake-sale", "kid-friendly"], nutrition: { calories: 180, protein: 2 }, ingredients: [{ name: "flour", amount: 2.25, unit: "cups" }, { name: "butter", amount: 1, unit: "cups" }, { name: "sugar", amount: 1.5, unit: "cups" }, { name: "chocolate chips", amount: 2, unit: "cups" }] },
{ _id: 24, name: "Teriyaki Salmon", cuisine: "Japanese", meal: "dinner", minutes: 25, servings: 2, vegetarian: false, rating: 4.4, tags: ["high-protein"], nutrition: { calories: 380, protein: 34 }, ingredients: [{ name: "salmon", amount: 1, unit: "lb" }, { name: "soy sauce", amount: 0.25, unit: "cups" }, { name: "honey", amount: 2, unit: "tbsp" }, { name: "garlic", amount: 2, unit: "cloves" }] },
{ _id: 25, name: "Bibimbap", cuisine: "Korean", meal: "dinner", minutes: 50, servings: 4, vegetarian: false, tags: ["spicy"], nutrition: { calories: 590, protein: 29 }, ingredients: [{ name: "rice", amount: 2, unit: "cups" }, { name: "beef", amount: 0.5, unit: "lb" }, { name: "spinach", amount: 2, unit: "cups" }, { name: "eggs", amount: 4, unit: "whole" }] },
{ _id: 26, name: "Ratatouille", cuisine: "French", meal: "dinner", minutes: 75, servings: 6, vegetarian: true, rating: 4.2, tags: ["vegan", "make-ahead"], nutrition: { calories: 180, protein: 4 }, ingredients: [{ name: "eggplant", amount: 1, unit: "whole" }, { name: "zucchini", amount: 2, unit: "whole" }, { name: "tomatoes", amount: 4, unit: "whole" }, { name: "garlic", amount: 3, unit: "cloves" }] },
{ _id: 27, name: "Huevos Rancheros", cuisine: "Mexican", meal: "breakfast", minutes: 25, servings: 2, vegetarian: true, rating: 4.5, tags: ["spicy"], nutrition: { calories: 430, protein: 19 }, ingredients: [{ name: "eggs", amount: 4, unit: "whole" }, { name: "tortillas", amount: 4, unit: "whole" }, { name: "black beans", amount: 1, unit: "cans" }, { name: "salsa", amount: 1, unit: "cups" }] },
{ _id: 28, name: "Mango Sticky Rice", cuisine: "Thai", meal: "dessert", minutes: 40, servings: 4, vegetarian: true, rating: 4.6, tags: ["sweet", "vegan"], nutrition: { calories: 450, protein: 5 }, ingredients: [{ name: "sticky rice", amount: 1.5, unit: "cups" }, { name: "coconut milk", amount: 1, unit: "cans" }, { name: "mango", amount: 2, unit: "whole" }] },
{ _id: 29, name: "Minestrone", cuisine: "Italian", meal: "lunch", minutes: 55, servings: 8, vegetarian: true, rating: 3.7, tags: ["soup", "make-ahead"], nutrition: { calories: 230, protein: 10 }, ingredients: [{ name: "pasta", amount: 1, unit: "cups" }, { name: "kidney beans", amount: 1, unit: "cans" }, { name: "tomatoes", amount: 1, unit: "cans" }, { name: "carrots", amount: 2, unit: "whole" }] },
{ _id: 30, name: "Green Curry", cuisine: "Thai", meal: "dinner", minutes: 35, servings: 4, vegetarian: false, rating: 4.3, tags: ["spicy"], nutrition: { calories: 500, protein: 30 }, ingredients: [{ name: "chicken", amount: 1, unit: "lb" }, { name: "coconut milk", amount: 1, unit: "cans" }, { name: "curry paste", amount: 3, unit: "tbsp" }, { name: "basil", amount: 0.5, unit: "cups" }] },
{ _id: 31, name: "Apple Crumble", cuisine: "British", meal: "dessert", minutes: 55, servings: 6, vegetarian: true, rating: 4.4, tags: ["sweet"], nutrition: { calories: 340, protein: 4 }, ingredients: [{ name: "apples", amount: 6, unit: "whole" }, { name: "flour", amount: 1, unit: "cups" }, { name: "oats", amount: 1, unit: "cups" }, { name: "butter", amount: 0.5, unit: "cups" }] },
{ _id: 32, name: "Overnight Oats", cuisine: "American", meal: "breakfast", minutes: 5, servings: 1, vegetarian: true, rating: 3.6, tags: ["make-ahead", "no-cook"], nutrition: { calories: 310, protein: 12 }, ingredients: [{ name: "oats", amount: 0.5, unit: "cups" }, { name: "milk", amount: 0.5, unit: "cups" }, { name: "yogurt", amount: 0.25, unit: "cups" }, { name: "honey", amount: 1, unit: "tbsp" }] },
{ _id: 33, name: "Falafel Wraps", cuisine: "Middle Eastern", meal: "lunch", minutes: 40, servings: 4, vegetarian: true, rating: 4.5, tags: ["vegan"], nutrition: { calories: 520, protein: 17 }, ingredients: [{ name: "chickpeas", amount: 2, unit: "cans" }, { name: "flatbread", amount: 4, unit: "whole" }, { name: "garlic", amount: 3, unit: "cloves" }, { name: "lemon", amount: 1, unit: "whole" }] },
{ _id: 34, name: "Beef Tacos", cuisine: "Mexican", meal: "dinner", minutes: 30, servings: 4, vegetarian: false, rating: 4.1, tags: ["kid-friendly"], nutrition: { calories: 540, protein: 31 }, ingredients: [{ name: "ground beef", amount: 1, unit: "lb" }, { name: "tortillas", amount: 8, unit: "whole" }, { name: "cheddar", amount: 1, unit: "cups" }, { name: "salsa", amount: 0.5, unit: "cups" }] },
{ _id: 35, name: "Hummus", cuisine: "Middle Eastern", meal: "snack", minutes: 10, servings: 6, vegetarian: true, rating: null, tags: ["vegan", "no-cook"], nutrition: { calories: 190, protein: 6 }, ingredients: [{ name: "chickpeas", amount: 1, unit: "cans" }, { name: "tahini", amount: 0.25, unit: "cups" }, { name: "lemon", amount: 1, unit: "whole" }, { name: "garlic", amount: 2, unit: "cloves" }] },
{ _id: 36, name: "Spanakopita", cuisine: "Greek", meal: "dinner", minutes: 70, servings: 8, vegetarian: true, tags: ["make-ahead"], nutrition: { calories: 360, protein: 14 }, ingredients: [{ name: "spinach", amount: 1, unit: "lb" }, { name: "feta", amount: 8, unit: "oz" }, { name: "phyllo", amount: 16, unit: "sheets" }, { name: "eggs", amount: 2, unit: "whole" }] },
{ _id: 37, name: "Shoyu Ramen", cuisine: "Japanese", meal: "dinner", minutes: 120, servings: 4, vegetarian: false, rating: 4.7, tags: ["soup", "noodles"], nutrition: { calories: 530, protein: 26 }, ingredients: [{ name: "ramen noodles", amount: 1, unit: "lb" }, { name: "chicken broth", amount: 8, unit: "cups" }, { name: "soy sauce", amount: 0.25, unit: "cups" }, { name: "eggs", amount: 4, unit: "whole" }] },
{ _id: 38, name: "Kimchi Fried Rice", cuisine: "Korean", meal: "lunch", minutes: 20, servings: 2, vegetarian: false, rating: 4.2, tags: ["spicy", "leftovers"], nutrition: { calories: 420, protein: 14 }, ingredients: [{ name: "rice", amount: 2, unit: "cups" }, { name: "kimchi", amount: 1, unit: "cups" }, { name: "eggs", amount: 2, unit: "whole" }] },
{ _id: 39, name: "Churros", cuisine: "Mexican", meal: "dessert", minutes: 45, servings: 6, vegetarian: true, tags: ["sweet"], nutrition: { calories: 260, protein: 3 }, ingredients: [{ name: "flour", amount: 1, unit: "cups" }, { name: "water", amount: 1, unit: "cups" }, { name: "eggs", amount: 2, unit: "whole" }, { name: "sugar", amount: 0.5, unit: "cups" }] },
{ _id: 40, name: "Tomato Soup", cuisine: "American", meal: "lunch", minutes: 30, servings: 4, vegetarian: true, rating: 3.9, tags: ["soup", "kid-friendly"], nutrition: { calories: 220, protein: 4 }, ingredients: [{ name: "tomatoes", amount: 2, unit: "cans" }, { name: "onion", amount: 1, unit: "whole" }, { name: "butter", amount: 4, unit: "tbsp" }, { name: "cream", amount: 0.5, unit: "cups" }] }
]);
Save it, and load it at the mongosh prompt. Like the file in Adding Many Documents with insertMany, it starts with db.recipes.drop(), so it replaces the recipes already there and is safe to run twice:
load("sunday-kitchen-full-recipes.js")
true
Here's one card in full:
db.recipes.findOne({ _id: 8 })
{
_id: 8,
name: 'Miso Soup',
cuisine: 'Japanese',
meal: 'lunch',
minutes: 15,
servings: 2,
vegetarian: true,
rating: 3.9,
tags: [ 'soup' ],
nutrition: { calories: 90, protein: 7 },
ingredients: [
{ name: 'miso paste', amount: 3, unit: 'tbsp' },
{ name: 'tofu', amount: 7, unit: 'oz' },
{ name: 'green onions', amount: 2, unit: 'whole' }
]
}
nutrition is an embedded document: a document inside a document, in its own braces, as Arrays and Documents Inside Documents showed. ingredients is a list of documents, which Arrays of Documents and $elemMatch takes on. This lesson is about nutrition. Every lesson in this chapter uses these 40 recipes, and loading the file again puts them back if you change them.
A field inside a field
Sunday Kitchen wants a "light bites" list: recipes under 200 calories. A first try names calories as if it were on the card's front:
db.recipes.countDocuments({ calories: { $lt: 200 } })
0
Zero, and no error. No recipe has a calories field of its own: each has a nutrition field, and calories is inside it. MongoDB looked for a field that isn't there, so nothing matched.
Think of a mailing address. "Apartment 4" isn't enough on its own; you say the building first, then the apartment inside it. Dot notation does the same for fields: the outer field, a dot, then the field inside it. "nutrition.calories" is read aloud as "nutrition dot calories":
db.recipes.find({ "nutrition.calories": { $lt: 200 } }, { name: 1, nutrition: 1 })
[
{ _id: 8, name: 'Miso Soup', nutrition: { calories: 90, protein: 7 } },
{
_id: 20,
name: 'Guacamole',
nutrition: { calories: 160, protein: 2 }
},
{
_id: 23,
name: 'Chocolate Chip Cookies',
nutrition: { calories: 180, protein: 2 }
},
{
_id: 26,
name: 'Ratatouille',
nutrition: { calories: 180, protein: 4 }
},
{ _id: 35, name: 'Hummus', nutrition: { calories: 190, protein: 6 } }
]
Read the filter as "recipes whose nutrition's calories are less than 200". Every operator from Comparison Operators: $gt, $lt, $in and More works on a dotted field the same way it works on a field of its own. A dotted field can sit beside other fields, too. High-protein dinners, 30 grams or more:
db.recipes.find({ meal: "dinner", "nutrition.protein": { $gte: 30 } }, { name: 1, "nutrition.protein": 1 })
[
{ _id: 2, name: 'Weeknight Chili', nutrition: { protein: 32 } },
{ _id: 10, name: 'Chicken Tikka Masala', nutrition: { protein: 41 } },
{ _id: 12, name: 'Spaghetti Carbonara', nutrition: { protein: 30 } },
{ _id: 14, name: 'Beef Pho', nutrition: { protein: 35 } },
{ _id: 24, name: 'Teriyaki Salmon', nutrition: { protein: 34 } },
{ _id: 30, name: 'Green Curry', nutrition: { protein: 30 } },
{ _id: 34, name: 'Beef Tacos', nutrition: { protein: 31 } }
]
The second pair of braces uses dot notation too, to show only the protein. Choosing Fields with Projections explains that part.

Why the quotes
Leave the quotes off, and mongosh stops before it asks the database anything:
db.recipes.find({ nutrition.calories: { $lt: 200 } })
Uncaught:
SyntaxError: Unexpected token, expected "," (1:27)
> 1 | db.recipes.find({ nutrition.calories: { $lt: 200 } })
| ^
2 |
mongosh reads your commands the way JavaScript does, and a field name without quotes can't have a dot in it: mongosh expected a comma after nutrition, the ^ under the line points at the dot, and the error is a SyntaxError, a mistake in how the command is written. Inside quotes, the dot is part of the name. Double quotes or single quotes both work; this course uses double quotes.
Matching the whole nutrition document
There's another way to reach inside: give the whole embedded document as the value. It works, as long as you match it exactly:
db.recipes.find({ nutrition: { calories: 90, protein: 7 } }, { name: 1 })
[ { _id: 8, name: 'Miso Soup' } ]
Now count the recipes that match the same two numbers written in the other order:
db.recipes.countDocuments({ nutrition: { protein: 7, calories: 90 } })
0
None. A whole-document filter asks for an embedded document that is exactly this one: the same fields, the same values, in the same order, and no others. Leave out protein, and it finds nothing either. If Sunday Kitchen adds fiber to every card next month, the first filter stops finding Miso Soup, with no error to warn you.
Dot notation tests only the fields you name, so it keeps working whatever else is in nutrition, in any order:
db.recipes.find({ "nutrition.protein": 7, "nutrition.calories": 90 }, { name: 1 })
[ { _id: 8, name: 'Miso Soup' } ]
Reach for dot notation by habit. Match a whole embedded document only when you mean "exactly this, nothing more", which is rare.
Check yourself
2 questions to try on the lesson page.
So what
Real documents nest: addresses in customers, details in products, nutrition in recipes. With dot notation in quotes, a filter can test any field inside them, and it keeps working when the embedded document grows a new field.