Sikau

MongoDB · Day 5 · about 35 min

Project: Recipe catalogue

Insert, query and update documents together.

A real collection

Create a recipes collection with nested ingredients, then answer questions and make changes with the operators from this unit.

Shape the recipe document

This project is small on purpose. Practising the full cycle of inserting, querying, projecting, updating and deleting on one collection builds the habits you'll rely on in larger apps, where the same operators appear in every feature.

A recipe is read as a whole — title, time, tags and ingredients all appear on the same screen — so it's one document. Ingredients are an array of small objects, each with an item and a quantity, because they belong to the recipe and the list is short. Tags are a plain array of strings, which MongoDB matches element by element.

db.recipes.insertOne({
  title: "Momo",
  minutes: 60,
  difficulty: "medium",
  tags: ["nepali", "steamed"],
  ingredients: [
    { item: "flour", qty: 2, unit: "cups" },
    { item: "cabbage", qty: 1, unit: "head" },
  ],
});

The rest of this lesson

  • Questions you'll answerexplain
  • Check your workexplain
  • Query for each questionmatch
  • Adding a tag twicepredict
  • Recipeschallenge
  • Badge quizquiz
  • Unit 1 complete!recap

Continue this lesson free — run real code in your browser

Continue with Google

See all 15 lessons of MongoDB