AWS Database Blog
Getting Started with Amazon DocumentDB (with MongoDB compatibility); Part 2 – using AWS Cloud9
Amazon DocumentDB (with MongoDB compatibility) is a fast, scalable, highly available, and fully managed document database service that supports MongoDB workloads. You can use the same MongoDB 3.6, 4.0 or 5.0 application code, drivers, and tools to run, manage, and scale workloads on Amazon DocumentDB without having to worry about managing the underlying infrastructure. As a document database, Amazon DocumentDB makes it easy to store, query, and index JSON data.
In part 2 of this series, this post shows you how to get started with Amazon DocumentDB with an AWS Cloud9 environment. The first step is to create an AWS Cloud9 environment and an Amazon DocumentDB cluster in your default Amazon VPC. For instructions on creating a default VPC, see Getting Started with Amazon VPC. This post demonstrates how to connect to your Amazon DocumentDB cluster from your AWS Cloud9 environment with a mongo shell and run a few queries. The walkthrough costs less than $0.10 to complete. When creating AWS resources, we recommend that you follow the AWS IAM best practices. For more information on getting started with Amazon DocumentDB from your local machine, see Getting started with Amazon DocumentDB (with MongoDB compatibility); Part 1 – using Amazon EC2.
The following diagram shows the final architecture of this walkthrough.
For this walkthrough, use the default VPC in a given Region. For more information, see Creating a Virtual Private Cloud (VPC).
Creating an AWS Cloud9 environment
To create your AWS Cloud9 environment, complete the following steps:
- Using the AWS management console, on the AWS Cloud9 management console, choose Create environment.
- Under Environment name and description, for Name, enter a name for the environment.
This post enters the nameDocumentDBCloud9
.
- Choose Next step.
- In the Configure settings section, accept all defaults.
- Choose Next step.
- In the Review section, choose Create environment.
The provisioning of the AWS Cloud9 environment can take up to three minutes. When it is complete, you see the following command prompt:
You are redirected to the command prompt to install the mongo shell and connect to your Amazon DocumentDB cluster.
Creating a security group
In this step, you create a new security group that enables you to connect to your Amazon DocumentDB cluster on port 27017 (the default port for Amazon DocumentDB) from your AWS Cloud9 environment. Complete the following steps:
- On the Amazon EC2 console, under Network & Security, choose Security groups.
- Choose Create security group.
- For Security group name, enter
demoDocDB
. - For VPC, accept the usage of your default VPC
- For Description, enter a description.
- In the Inbound rules section, choose Add rule.
- For Type, choose Custom TCP Rule.
- For Port Range, enter
27017
.
The source security group is the security group for the AWS Cloud9 environment you just created. - To see a list of available security groups, enter
cloud9
in the destination field. - Choose the security group with the name
aws-cloud9-<environment name>
. - Accept all other defaults and choose Create security group.
The following screenshot shows you the security groups that were created in this step as well as the AWS Cloud9 security group that was created when you created an AWS Cloud9 environment.
Creating an Amazon DocumentDB cluster
To create your Amazon DocumentDB cluster, complete the following steps:
- On the Amazon DocumentDB management console, under Clusters, choose Create.
- On the Create Amazon DocumentDB cluster page, for Instance class, select db.t3.medium.
- For Number of instances, choose 1.
This helps minimize costs. - Leave other settings at their default.
- In the Authentication section, enter a username and password.
- Turn on Show advanced settings.
- In the Network settings section, for VPC security groups, choose demoDocDB.
- Choose Create cluster.
Amazon DocumentDB is now provisioning your cluster, which can take up to a few minutes to finish. You can connect to your cluster when both the cluster and instance status show as Available. While Amazon DocumentDB provisions the cluster, complete the remaining steps to connect to your Amazon DocumentDB cluster.
Installing the mongo shell
You can now install the mongo shell, which is a command-line utility that you use to connect and query your Amazon DocumentDB cluster.
To install the mongo shell, complete the following steps:
If your AWS Cloud9 environment is still open, you can skip to step 3.
- On the AWS Cloud9 management console, under Your environments, choose DocumentDBCloud9.
- Choose open IDE.
- At the command prompt, create the repository file with the following code:
- When it is complete, install the mongo shell with the following code:
Transport Layer Security (TLS) is enabled by default for any new Amazon DocumentDB clusters. For more information, see Managing Amazon DocumentDB Cluster TLS Settings.
- To encrypt data in transit, download the CA certificate for Amazon DocumentDB. See the following code:
You are now ready to connect to your Amazon DocumentDB cluster.
Connecting to your Amazon DocumentDB cluster
- On the Amazon DocumentDB console, under Clusters, locate your cluster.
This post uses the clusterdocdb-2020-02-08-14-15-11
.
- Choose the cluster you created.
- Copy the connection string provided.
Omit <insertYourPassword> so that you are prompted for the password by the mongo shell when you connect. This way, you don’t have to type your password in cleartext.
Your connection string should look like the following code:
When you enter your password and can see thers0:PRIMARY>
prompt, you are successfully connected to your Amazon DocumentDB cluster. For information about troubleshooting, see Troubleshooting Amazon DocumentDB.
Inserting and querying data
Now that you are connected to your cluster, you can run a few queries to get familiar with using a document database.
To insert a single document, enter the following code:
You get the following output:
You can read the document that you wrote with the findOne()
command (because it only returns a single document). See the following code:
You get the following output:
To perform a few more queries, consider a gaming profiles use case. First, insert a few entries into a collection entitled profiles
. See the following code:
You get the following output:
Use the find()
command to return all the documents in the profiles collection. See the following code:
db.profiles.find()
You get the following output:
Use a query for a single document using a filter. See the following code.
You get the following output:
A common use case in gaming is finding a profile for a given user and increment a value in the user’s profile. In this scenario, you want to run a promotion for the top active gamers. If the gamer fills out a survey, you increase their score by +10. To do that, use the findAndModify
command. In this use case, the user Tim received and completed a survey. To give Tim the credit to their score, enter the following code:
You get the following output:
You can verify the result with the following query:
You get the following output:
Cleaning up
When you complete the walkthrough, you can either stop your Amazon DocumentDB cluster to reduce costs or delete the cluster. By default, after 30 minutes of inactivity, your AWS Cloud9 environment stops the underlying EC2 instance to help save costs.
Summary
This post showed you how to get started with Amazon DocumentDB by creating an AWS Cloud9 environment, installing the mongo shell, creating an Amazon DocumentDB cluster, connecting to your cluster, and performing a few queries to see how easy it is to insert and query JSON documents within Amazon DocumentDB. For more information, see Ramping up on Amazon DocumentDB (with MongoDB compatibility). For more information about recent launches and blog posts, see Amazon DocumentDB (with MongoDB compatibility) resources.
About the Authors
Joseph Idziorek is a Principal Product Manager at Amazon Web Services.
Aleksandr Iziumov is a Solutions Architect at Amazon Web Services.