Get started with Amazon DocumentDB elastic clusters
This getting started section walks you through on how you can create and query your first elastic cluster. There are many ways to connect and get started with elastic clusters. This guide uses Amazon Elastic Compute Cloud (Amazon EC2) to connect and query your Amazon DocumentDB elastic cluster directly from the AWS Management Console.
Topics
Setting Up
If you would rather connect to your Amazon DocumentDB from your local machine by creating an SSH connection to an Amazon EC2 instance, please see Connecting with Amazon EC2.
Prerequisites
Before you create your first Amazon DocumentDB cluster, you must do the following:
- Create an Amazon Web Services (AWS) account
-
Before you can begin using Amazon DocumentDB, you must have an Amazon Web Services (AWS) account. The AWS account is free. You pay only for the services and resources that you use.
If you do not have an AWS account, complete the following steps to create one.
To sign up for an AWS account
Follow the online instructions.
Part of the sign-up procedure involves receiving a phone call and entering a verification code on the phone keypad.
When you sign up for an AWS account, an AWS account root user is created. The root user has access to all AWS services and resources in the account. As a security best practice, assign administrative access to a user, and use only the root user to perform tasks that require root user access.
- Set up the needed AWS Identity and Access Management (IAM) permissions.
-
Access to manage Amazon DocumentDB resources such as clusters, instances, and cluster parameter groups requires credentials that AWS can use to authenticate your requests. For more information, see Identity and Access Management for Amazon DocumentDB.
-
In the search bar of the AWS Management Console, type in IAM and select IAM in the drop down menu.
-
Once you're in the IAM console, select Users from the navigation pane.
-
Select your username.
-
Click the button Add permissions.
-
Select Attach policies directly.
-
Type
AmazonDocDBFullAccess
in the search bar and select it once it appears in the search results. -
Click the Next button.
-
Click the Add permissions button.
-
- Create an Amazon Virtual Private Cloud (Amazon VPC)
-
Your AWS account includes a default VPC in each region. This step is only necessary if you choose to use a default Amazon VPC. In this case, complete the steps in the Create a Amazon VPC topic in the Amazon VPC User Guide.
- Launch an Amazon EC2 instance
-
Complete steps 1 and 2 of the Get started with Amazon EC2 topic in the Amazon Elastic Compute Cloud User Guide.
Note
Record the name and ID of the security group created for your Amazon EC2 instance.
Step 1: Create an elastic cluster
In this section we explain how to create a brand new elastic cluster, using either the AWS Management Console or AWS CLI with the following instructions.
Step 2: Enable inbound connections
Access to an Amazon DocumentDB cluster is controlled by it’s associated security group. In this step you will modify the Amazon DocumentDB security group to allow communication from the Amazon EC2 instance through the port that’s configured with the elastic cluster (default 27017).
-
On the Amazon DocumentDB management console, under Clusters, locate your cluster.
Choose the cluster you created by clicking on the cluster identifier.
In the Connectivity & security tab, in the Security section, choose VPC Security Groups.
This opens the Amazon EC2 management console Security Groups list. Alternatively, you can access the Amazon EC2 management console, select Security Groups from the Network and Security section.
Choose the Amazon DocumentDB (default) security group.
-
Choose the Inbound rules tab (you may need to scroll to the bottom of the window), then choose Edit inbound rules.
-
In the Edit inbound rules dialog, select Add rule.
For Port range, enter
27017
.Leave Source as Custom and search for the security group value you recorded from your EC2 instance you created in Prerequisites.
Choose Save rules.
Step 3: Install the mongo shell
Install the mongo shell in your Amazon EC2 instance that you created in Prerequisites. The mongo shell is a command-line utility that you use to connect and query your elastic cluster.
Connect to your Amazon EC2 instance and at the command prompt, create the repository file with the following command:
echo -e "[mongodb-org-5.0] \nname=MongoDB Repository\nbaseurl=https://repo.mongodb.org/yum/amazon/2023/mongodb-org/5.0/x86_64/\ngpgcheck=1 \nenabled=1 \ngpgkey=https://pgp.mongodb.com/server-5.0.asc" | sudo tee /etc/yum.repos.d/mongodb-org-5.0.repo
When it is complete, install the mongo shell with the following command:
sudo yum install -y mongodb-org-shell
Step 4: Connect to your new elastic cluster
Connect to your cluster using the mongo shell that you installed in Step 2.
-
On the Amazon DocumentDB Management Console, under Clusters, locate your cluster. Sort by role to display all clusters with the role Elastic Cluster.
-
Choose the cluster you created by selecting the cluster identifier. From Connectivity and Security, copy your endpoint and paste it into your Amazon EC2 instance.
-
Once connected, you should see something similar to the following output:
Step 5: Shard your collection; insert and query data
Elastic clusters add support for sharding in Amazon DocumentDB. Now that you are connected to your cluster, you can shard the cluster, insert data and run a few queries.
-
To shard a collection, enter the following:
sh.shardCollection("db.Employee1" , { "Employeeid" : "hashed" })
-
To insert a single document, enter the following:
db.Employee1.insert({"Employeeid":1, "Name":"Joe", "LastName": "Bruin", "level": 1 })
The following output is displayed:
WriteResult({ "nInserted" : 1 })
-
To read the document that you wrote, enter the
findOne()
command (it returns a single document):db.Employee1.findOne()
The following output is displayed:
{ "_id" : ObjectId("61f344e0594fe1a1685a8151"), "EmployeeID" : 1, "Name" : "Joe", "LastName" : "Bruin", "level" : 1 }
-
To perform a few more queries, consider a gaming profile use case. First, insert a few entries into a collection titled "Employee". Enter the following:
db.Employee1.insertMany([ { "Employeeid" : 1, "name" : "Matt", "lastname": "Winkle", "level": 12}, { "Employeeid" : 2, "name" : "Frank", "lastname": "Chen", "level": 2}, { "Employeeid" : 3, "name" : "Karen", "lastname": "William", "level": 7}, { "Employeeid" : 4, "name" : "Katie", "lastname": "Schaper", "level": 3} ])
The following output is displayed:
{ "acknowledged" : true, "insertedIds" : [ 1, 2, 3, 4 ] }
-
To return all the documents in the profiles collection, enter the
find
() command:db.Employee1.find()
The data you entered in step 4 is displayed.
-
To query a single document, include a filter (for example: "Katie"). Enter the following:
db.Employee1.find({name: "Katie"})
The following output is displayed:
{ "_id" : 4, "name" : "Katie", "lastname": "Schaper", "level": 3}
-
To find a profile and modify it, enter the
findAndModify
command. In this example, the employee "Matt" is given a higher level of "14":db.Employee1.findAndModify({ query: { "Employeeid" : 1, "name" : "Matt"}, update: { "Employeeid" : 1, "name" : "Matt", "lastname" : "Winkle", "level" : 14 } })
The following output is displayed (note that the level has not changed yet):
{ "_id" : 1, "name" : "Matt", "lastname" : "Winkle", "level" : 12, }
-
To verify the level increase, enter the following query:
db.Employee1.find({name: "Matt"})
The following output is displayed:
{ "_id" : 1, "name" : "Matt", "lastname" : "winkle", "level" : 14 }