Checkpoints
Create Configuration
/ 100
Getting Started with MongoDB Atlas on Google Cloud
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.
GSP1093
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:
- Through the Google Cloud Marketplace if you have a Google Cloud account. Refer to the documentation to set up your subscription.
- With the Atlas registration page.
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).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
How to start your lab and sign in to the Google Cloud console
-
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
-
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. -
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.
-
Click Next.
-
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.
-
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. -
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.
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.
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.
-
Click Database from the left side menu, under the DEPLOYMENT heading.
-
Click on the Build a Database button.
-
Select Shared.
-
Select Google Cloud as your Cloud Provider and choose a Region that's closest to your physical location.
-
Rename your cluster to Workshop-Bakery.
-
Click Create to deploy your cluster.
-
To navigate back to your deployments, select Database from the left-hand menu.
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.
-
Click the Browse Collections button to access the Workshop-Bakery cluster.
-
In the window that appears, select Add My Own Data.
-
Name the database Bakery and the collection cakes, then click Create.
Now the cakes
collection has been created inside the new bakery
database. Time to add some data. The Atlas UI makes this really easy.
-
Click the Insert Document button in the top right corner.
-
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.
-
Delete what is currently in the box, add the following cake document, and then press Insert:
- Repeat steps 4 and 5, this time adding the following cake document:
-
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. -
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 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.
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.
-
Start by navigating to the App Services Tab.
-
You’ll be prompted to select a starter template. Select the Build your own App option that’s already selected. Click the Next button.
- 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.
- 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.
-
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.
-
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.
-
Select the comments collection from the left, and this time, apply the readAndWriteAll rule.
-
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.
-
Click Schema on the left.
-
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.
-
Leave the default sample size and click Generate schema from sampling.
-
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.
-
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. -
Click on json view {}.
-
Paste the following JSON schema into the box.
Then click Save Draft.
Now define a relationship between the two collections so that the comment document understands cakeId
.
-
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.
-
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.
-
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.
-
Click GraphQL from the left side menu, under the Build heading.
-
Replace the comments and sample query inside the playground with the following query, which requests the name and description from your
cakes
collection:
-
Click the play button at the top to run your query, and see your cakes document returned on the right.
Task 4. Obtain an API key and GraphQL endpoint from MongoDB Atlas:
-
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.
-
Enable the
Provider Enabled
and click Save Draft. -
Click Review Draft & Deploy.
-
Click Deploy in the modal that pops up.
-
Click Create API Key.
-
Name your key apikey and click Create to create the API key.
-
Copy the created API key. This will be needed for a later step.
-
Now, navigate to the GraphQL section and copy the GraphQL endpoint. This too will be used in the later step.
Task 5. Verify the lab in Google Cloud
-
In the Google Cloud console go to Cloud Functions. The Cloud Function mongodb_graphql has already been created for you - click on the function.
-
Click Edit to add in your environment variables.
-
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.
-
Then click Next. Then click Deploy.
-
Go back to the Cloud Functions page. Click Test function under Actions. Then again click on Test the Function.
-
Look at the output. Output will be the data.
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.