Skip to main content

Prepare Your Data

In the previous section, you deployed a free database cluster named Workshop-Bakery. As you may have guessed from the cluster name, you will be building a baking recipes website. Hence, in this section you will insert some data containing delicious cake recipes into your database.

info

You will be using the Atlas UI again. However, you can also browse and manage MongoDB data with the MongoDB Compass GUI and the MongoDB Shell.

  1. Click the Browse Collections button to access your new Workshop-Bakery cluster.

    Database deployment section highlighting the 'Browse Collections' button
  2. In the window that appears, select Add My Own Data.

    'Add data to your Collections' dialog highlighting the 'Add My Own Data' button
  3. Name the database Bakery and the collection cakes. Click Create.

    'Create Database' dialog with 'Bakery' filled in as a database name and 'cakes' filled in as a collection name
  4. You now have a cakes collection created inside the new bakery database—woo! Next, you will add some data. The Atlas UI makes this really easy. Click the Insert Document button in the top right corner.

    Cakes collection page highlighting the 'Insert Document' button
  5. In the Atlas UI, there are two options for adding data when inserting it. You can either add each field individually or define the data as a JSON object. In this case, you will use the JSON view and copy-paste an existing cake recipe. To switch to the JSON view, click the {} button.

    'Insert to Collection' dialog highlighting the JSON button
  6. Delete what is currently in the box, copy and paste the following cake document, and then click Insert:

    {
    "_id": {
    "$oid": "64759d513beb40594cb859e9"
    },
    "name": "Chocolate Cake",
    "shortDescription": "Chocolate cake is a cake flavored with melted chocolate, cocoa powder, or sometimes both.",
    "description": "Chocolate cake is made with chocolate; it can be made with other ingredients, as well. These ingredients include fudge, vanilla creme, and other sweeteners. The history of chocolate cake goes back to 1764, when Dr. James Baker discovered how to make chocolate by grinding cocoa beans between two massive circular millstones.",
    "image": "https://addapinch.com/wp-content/uploads/2020/04/chocolate-cake-DSC_1768.jpg",
    "ingredients": [
    "flour",
    "sugar",
    "cocoa powder"
    ],
    "recipe": "Preheat your oven to the specified temperature and prepare a greased cake pan. Mix the dry and wet ingredients separately, then combine and pour the batter into the pan. Bake for the recommended time, let it cool, and optionally frost or decorate as desired.",
    "stock": 25
    }
  7. Repeat Steps 4 and 5, this time adding the following cake document:

    {
    "_id": {
    "$oid":"64759e4c3beb40594cb859ed"
    },
    "name": "Cheese Cake",
    "shortDescription": "Cheesecake is a sweet dessert consisting of one or more layers. The main, and thickest, layer consists of a mixture of a soft, fresh cheese (typically cottage cheese, cream cheese or ricotta), eggs, and sugar. ",
    "description": "Cheesecake is a sweet dessert consisting of one or more layers. The main, and thickest, layer consists of a mixture of a soft, fresh cheese (typically cottage cheese, cream cheese or ricotta), eggs, and sugar. If there is a bottom layer, it most often consists of a crust or base made from crushed cookies (or digestive biscuits), graham crackers, pastry, or sometimes sponge cake.[1] Cheesecake may be baked or unbaked (and is usually refrigerated).",
    "image":"https://sallysbakingaddiction.com/wp-content/uploads/2018/05/perfect-cheesecake-recipe.jpg",
    "ingredients": [ "graham cracker crumbs", "sugar", "eggs", "butter", "sour cream", "cream cheese", "vanilla extract" ],
    "recipe": "Mix graham cracker crumbs and melted butter for the crust. Press into a pan. Beat cream cheese, sugar, and vanilla. Fold in whipped cream. Pour over the crust. Refrigerate until set, remove from pan, and serve chilled with desired toppings.",
    "stock": 40
    }
    info

    MongoDB documents use BSON (Binary JSON), a binary serialization format, to store data. BSON extends the JSON specification with a few data types such as Date and BinData.

    One of the BSON types if ObjectId — an easy to generate 12-byte value that consists of a timestamp, a machine and a process identifiers, and a counter. In MongoDB, every document stored in a collection must have a unique _id field, which serves as a primary key. If a document is inserted without specifying an _id field, the MongoDB driver will automatically generate an ObjectId for the _id field.

  8. Next, you will create a new collection — comments that stores the reviews for each recipe left from users on the website. Hover your mouse over Bakery in the left panel and click on the + sign that shows up.

    Databases section with the '+' sign to add a new collection highlighted
  9. Name the collection comments and click Create.

    Create database dialog with the text 'comments' in the collection name field
  10. Add a new comment by copy-pasting the document below.

    {
    "_id": {
    "$oid": "6475be48a10dcef00d5c7d9c"
    },
    "cakeId": {
    "$oid": "64759d513beb40594cb859e9"
    },
    "name": "Peter Quill",
    "text": "This recipe was super easy to follow and the result was delicious!",
    "date": {
    "$date": {
    "$numberLong": "1685750400000"
    }
    }
    }
    info

    The cakeId field is of type ObjectId just like the _id field. _id is the unique identifier of this comment document.

    On the other hand, cakeId is equal to the _id of a document from the cakes. In MongoDB, this is called a manual reference. Later, you will use this field to join data from the two collections.

    You might also notice that date looks different than the rest of the fields. This field uses the BSON Date type which is a 64-bit integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970).

Congrats! You now have your Atlas database set up with some cake recipes, and even a comment associated with a cake!