arrow_back

Getting Started with MongoDB Atlas on Google Cloud

Join Sign in

Getting Started with MongoDB Atlas on Google Cloud

1 hour 1 Credit

This lab was developed with our partner, MongoDB. Your personal information may be shared with MongoDB, the lab sponsor, if you have opted-in to receive product updates, announcements, and offers in your Account Profile.

Note: This lab requires a partner account. Please follow the lab instructions to create your account before starting the lab.

GSP1093

Google Cloud self-paced labs logo

Overview

MongoDB Atlas is a developer data platform, integrating a multi-cloud database with a diverse set of data services. Atlas simplifies database deployment and management while enabling modern data-driven application development.

In this introductory-level lab, you will get hands-on practice with MongoDB Atlas. You will create your first forever-free cluster and set up App Services, including a GraphQL API. If you’re new to Atlas, you came to the right place!

Objectives

In this lab, you will do the following:

  • Provision your first MongoDB Atlas cluster
  • Create a database and collections with data
  • Set up App Services GraphQL API
  • Query data with the generated GraphQL API

Prerequisites

Since this lab is an introduction to MongoDB Atlas, you will need an Atlas account. Create an account prior to clicking Start Lab. You can do that in one of two ways:

Save this account information! You will need it for the MongoDB Atlas with Natural Language API and Cloud Run which you should take after this one.

Setup and requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

How to start your lab and sign in to the Google Cloud console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Cloud console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).

    The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username below and paste it into the Sign in dialog.

    {{{user_0.username | "Username"}}}

    You can also find the Username in the Lab Details panel.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

    {{{user_0.password | "Password"}}}

    You can also find the Password in the Lab Details panel.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  7. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Google Cloud console opens in this tab.

Note: To view a menu with a list of Google Cloud products and services, click the Navigation menu at the top-left. Navigation menu icon

Atlas setup

Prior to starting to clicking Start Lab on this lab, please create an Atlas account.

Sign-up for a free account at Atlas registration: account.mongodb.com/account/register.

Atlas login

Note : You will use the same mongodb account for lab "MongoDB Atlas with Natural Language API and Cloud Run". Please keep a note of your account credentials.

Task 1. Create an Atlas project and cluster

In this section, you will spin up your first cluster and database.

Provision a free cluster

Deploy your first forever-free database in MongoDB Atlas! You'll do that using the Atlas UI, so make sure you’re logged into your account.

  1. Click Database from the left side menu, under the DEPLOYMENT heading.

  2. Click on the Build a Database button.

    Create

Note : If you are not able to see Build a Database button then first delete the existing cluster which is created by fefault.
  1. Select Shared.

  2. Select Google Cloud as your Cloud Provider and choose a Region that's closest to your physical location.

    Cloud provider region

  3. Rename your cluster to Workshop-Bakery.

  4. Click Create to deploy your cluster.

  5. To navigate back to your deployments, select Database from the left-hand menu.

    Database deployments

Create a database and collection, and insert the first documents

In this section, you will browse the new Workshop-Bakery cluster, learn how to create a new database, add a collection to that database, and insert documents.

You will use the Atlas UI for this section. However, all this can also be done with the Atlas CLI and MongoDB Shell.

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

    Browse Collections

  2. In the window that appears, select Add My Own Data.

  3. Name the database Bakery and the collection cakes, then click Create.

    Create collection

Note: If you face an error while creating the database, logout and relogin with the mongoDB credentials.

Now the cakes collection has been created inside the new bakery database. Time to add some data. The Atlas UI makes this really easy.

  1. Click the Insert Document button in the top right corner.

    Insert document

  2. When inserting data in the Atlas UI, you have two options of the view you want for adding data. We want to use the JSON view, so we can easily add some existing cake data. Click the {} button to switch view.

    Insert to collection

  3. Delete what is currently in the box, add the following cake document, and then press Insert:

{ "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" ], "stock": 25 }
  1. Repeat steps 4 and 5, this time adding the following cake document:
{ "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":[ "flour", "sugar", "eggs" ], "stock": 40 }
  1. Start storing comments left on each cake and associating them with the cake where the comment was left. Copy the _id value from inside the quotes of one of the cake documents you just inserted.

    ID value

  2. Add a second collection to your bakery database to store the comments that people can leave on each cake. Hover your mouse over Bakery in the left panel and there will be a + sign to add a new collection. Click this and add a new collection called comments.

    Add collection

  3. Add a new document to this new comments collection, using the following template, replacing the <ID of Cake Document> with the one you copied in Step 8, and replace <NAME> and <COMMENT> with your name, and a comment.

{ "cakeId": "<ID of Cake Document>", "date": "...", "name":"<NAME>", "text":"<COMMENT>" }

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

Task 2. Configure a GraphQL API with Atlas App Services

In this section, create your first Atlas App Services application, set some rules for accessing the collections, and then configure a schema for your data, which allows you to enable GraphQL.

Create an Atlas App Services application

In this section, set up your first Atlas App Services application, ready for using GraphQL.

  1. Start by navigating to the App Services Tab.

    App Services

  2. You’ll be prompted to select a starter template. Select the Build your own App option that’s already selected. Click the Next button.

Note: You can skip further steps if the Atlas App Services application is already created.
  1. Now you need to configure your application.
    • Data Source: Since you have created a single cluster, Atlas already linked it to your application.
    • Application Name: Give your application a meaningful name such as Bakery.
    • App Deployment Model: Change the deployment to Single Region and select the region closest to your physical location.
  2. Click the Create App Service button to create your first App Services application!

Configure rules

Add some rules to control the app’s access to your data. You don’t need everything to be able to read and write. Configure those rules in this section.

  1. At the end of the previous section, after creating your application, it will have taken you to it in your browser. On the left, under the Data Access heading, select Rules.

    Rules

  2. Make two rules — one for each collection.

    • Select the cakes collection.
    • Select the readAll preset.
    • Click Add preset role.

A popup will appear talking about Save and Deploy. You can just click through this and leave it as it is.

  1. Select the comments collection from the left, and this time, apply the readAndWriteAll rule.

  2. Since you have made changes to the app by adding these rules, you need to deploy those changes. This Review Draft & Deploy step is on by default, to protect you from making changes unintentionally.

    • Click the Review Draft & Deploy button in the blue banner across the top of your screen.
    • This will show the rules you have applied, as the system has added them into a new rules.json file. Scroll down to the bottom of the window and click Deploy.

Task 3. Generate a schema

GraphQL requires you to have a schema defined for building up queries. In this section you will generate a schema for both the cakes and comments collections.

  1. Click Schema on the left.

  2. Select the cakes collection. App Services will see that you already have data that it can generate a schema from, so click Define a schema.

  3. Leave the default sample size and click Generate schema from sampling.

  4. A JSON schema will be generated, matching the field names to their data types. At the top of the browser window, click Save Draft to save this new schema.

  5. Now create a schema for the comments collection. This time, write the schema instead of generating it. Click Define a schema. Then click or,skip and manually define your own schema.

    Generate schema manually

  6. Click on json view {}.

  7. Paste the following JSON schema into the box.

{ "title":"comment", "properties":{ "_id":{ "bsonType":"objectId" }, "cakeId":{ "bsonType":"objectId" }, "date":{ "bsonType":"date" }, "name":{ "bsonType":"string" }, "text":{ "bsonType":"string" } } }

Then click Save Draft.

Now define a relationship between the two collections so that the comment document understands cakeId.

  1. Click Add Relationship and set the following:

    • Parent Field: cakeId - ObjectId
    • Linked Database: Bakery
    • Linked Collection: cakes
    • Linked Field: _id

    Click Add then click Save Draft.

  2. At this point, you need to click Review Draft & Deploy at the top of the browser window, to deploy both the new schemas for your application.

  3. Click Deploy in the modal that pops up.

Query data with GraphQL

Now that you have the schemas in place, it’s time to run the first query to check it all works.

  1. Click GraphQL from the left side menu, under the Build heading.

    GraphQL
  2. Replace the comments and sample query inside the playground with the following query, which requests the name and description from your cakes collection:

query { cakes { name description } }
  1. Click the play button at the top to run your query, and see your cakes document returned on the right.

    Return Query

Task 4. Obtain an API key and GraphQL endpoint from MongoDB Atlas:

Note: The following steps will verify if you have completed the lab successfully by querying your GraphQL data.
  1. In Atlas App services, click Authentication under the Data Access heading then go to Authentication Providers. Here from the list, you will click Edit beside API keys.

  2. Enable the Provider Enabled and click Save Draft.

  3. Click Review Draft & Deploy.

  4. Click Deploy in the modal that pops up.

    Deploy API key

  5. Click Create API Key.

  6. Name your key apikey and click Create to create the API key.

  7. Copy the created API key. This will be needed for a later step.

    Copy API key

  8. Now, navigate to the GraphQL section and copy the GraphQL endpoint. This too will be used in the later step.

    Copy GraphQL Endpoint

Task 5. Verify the lab in Google Cloud

  1. In the Google Cloud console go to Cloud Functions. The Cloud Function mongodb_graphql has already been created for you - click on the function.

  2. Click Edit to add in your environment variables.

    Edit function

  3. Expand Runtime, build, connections and security settings. Scroll to Runtime enviornment variables.

  • Beside API_KEY paste the key you have copied from the previous step.

  • Beside MONGODB_DOMAIN paste the GraphQL endpoint copied from the previous step.

    env variable

  • Then click Next. Then click Deploy.

  1. Go back to the Cloud Functions page. Click Test function under Actions. Then again click on Test the Function.

    test function

  2. Look at the output. Output will be the data.

Note : You will be using the database created here for the lab "MongoDB Atlas with Natural Language API and Cloud Run", so please make sure you do not delete any resources created during this lab in your MongoDB Atlas account. Query MongoDB database with GraphQL

Congratulations!

You have provisioned your first MongoDB Atlas cluster, created a database and a collection with data. You also learned how to access your data through a GraphQL API thanks to Atlas App Services.

Next Steps / Learn More

To keep learning MongoDB try these labs:

Be sure to check out MongoDB on the Google Cloud Marketplace!

Get free $500 credits for MongoDB on Google Cloud Marketplace - Applicable only for new customers

Google Cloud training and certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated November 02, 2023

Lab Last Tested November 06, 2023

Copyright 2023 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.