Get started with Amazon DocumentDB
There are many ways to connect and get started with Amazon DocumentDB. We created this guide because we found this way to be the quickest, simplest and easiest way for users to get started using our powerful document database. This guide uses Amazon Elastic Compute Cloud (Amazon EC2) to connect and query your Amazon DocumentDB cluster directly from the AWS Management Console. New customers who are eligible for the AWS Free Tier can use Amazon DocumentDB and Amazon EC2 for free. If your Amazon EC2 environment or Amazon DocumentDB cluster makes use of resources beyond the free tier, you are charged the normal AWS rates for those resources. This guide will get you started with Amazon DocumentDB in less than 15 minutes.
Note
The instructions in this guide are specifically for creating and connecting to Amazon DocumentDB instance-based clusters. If you want to create and connect to Amazon DocumentDB elastic clusters, see Get started with Amazon DocumentDB elastic clusters.
Topics
If you would rather connect to your Amazon DocumentDB from your local machine by creating an SSH connection to an Amazon EC2 instance, see Connect using 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
Open https://portal.aws.amazon.com/billing/signup
. 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 that appears.
-
Once you're in the IAM console, select Users from the navigation pane.
-
Select your username.
-
Click the button Add permissions.
-
Select Attach existing policies directly.
-
Type
AmazonDocDBFullAccess
in the search bar and select it once it appears in the search results. -
Click the blue button at the bottom that says Next: Review.
-
Click the blue button at the bottom that says Add permissions.
-
- 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.
Step 1: Create an Amazon DocumentDB cluster
In this step you will create an Amazon DocumentDB cluster.
-
On the Amazon DocumentDB management console, under Clusters, choose Create.
-
On the Create Amazon DocumentDB cluster page, in the Cluster type section, choose Instance Based Clusters (this is the default option).
-
In the Configuration section:
For Cluster identifier, enter a unique name, such as
mydocdbcluster
. Note that the console will change all cluster names into lower-case regardless of how they are entered.For Engine version, choose 5.0.0.
For Instance class, choose db.t3.medium. This is eligible for the AWS Free Tier.
For Number of instances, choose 1 instance. Choosing one instance helps minimize costs. If this were a production system, we would recommend that you provision three instances for high availability.
-
In the Cluster storage configuration section, choose Amazon DocumentDB Standard (this is the default option).
-
In the Connectivity section:
Select Connect to an EC2 compute resource.
For EC2 Instance, select the EC2 instance you created in Prerequisites.
-
In the Authentication section, enter sign-in credentials.
-
Leave all other options as default and choose Create cluster.
Note
When you set up a connection between an EC2 instance and an Amazon DocumentDB database, Amazon DocumentDB automatically configures the VPC security group for your EC2 instance and for your Amazon DocumentDB database. For more information, see Overview of automatic connectivity with an EC2 instance.
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.
Note
For information about cluster status values, see Cluster status values in the Monitoring Amazon DocumentDB chapter.
For information about instance status values, see Instance status values in the Monitoring Amazon DocumentDB chapter.
Step 2: 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 Amazon DocumentDB 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 3: Connect to your Amazon DocumentDB cluster
Connect to your Amazon DocumentDB cluster using the mongo shell that you installed in Step 2.
-
On the Amazon DocumentDB management console, under Clusters, locate your cluster. Choose the cluster you created by clicking on the cluster identifier.
-
Encryption-in-transit is enabled by default on Amazon DocumentDB. You can optionally disable TLS. To download the current certificate required to authenticate to your cluster, in the Connectivity & security tab, in the Connect section, under Download the Amazon DocumentDB Certificate Authority (CA) certificate required to authenticate to your cluster, copy the command provided. Go back to your Amazon EC2 instance and paste the command.
-
Return to your cluster in the Amazon DocumentDB console, under Connectivity & security tab, in the Connect section, under Connect to this cluster with the mongo shell, copy the connection string provided. Omit copying
<insertYourPassword>
so that you are prompted for the password by the mongo shell when you connect.Go back to your Amazon EC2 instance and paste the connection string.
When you enter your password and your prompt becomes rs0:PRIMARY>
, you are successfully connected to your Amazon DocumentDB cluster.
Note
For information about troubleshooting, see Troubleshooting Amazon DocumentDB.
Step 4: Insert and query 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:
db.collection.insert({"hello":"DocumentDB"})
-
You get the following output:
WriteResult({ "nInserted" : 1 })
-
You can read the document that you wrote with the
findOne()
command (because it only returns a single document). Input the following:db.collection.findOne()
-
You get the following output:
{ "_id" : ObjectId("5e401fe56056fda7321fbd67"), "hello" : "DocumentDB" }
-
To perform a few more queries, consider a gaming profiles use case. First, insert a few entries into a collection titled
profiles
. Input the following:db.profiles.insertMany([ { "_id" : 1, "name" : "Matt", "status": "active", "level": 12, "score":202}, { "_id" : 2, "name" : "Frank", "status": "inactive", "level": 2, "score":9}, { "_id" : 3, "name" : "Karen", "status": "active", "level": 7, "score":87}, { "_id" : 4, "name" : "Katie", "status": "active", "level": 3, "score":27} ])
-
You get the following output:
{ "acknowledged" : true, "insertedIds" : [ 1, 2, 3, 4 ] }
-
Use the
find()
command to return all the documents in the profiles collection. Input the following:db.profiles.find()
-
You will get an output that will match the data you typed in Step 5.
-
Use a query for a single document using a filter. Input the following:
db.profiles.find({name: "Katie"})
-
You should get back this output:
{ "_id" : 4, "name" : "Katie", "status": "active", "level": 3, "score":27}
-
Now let’s try to find a profile and modify it using the
findAndModify
command. We’ll give the user Matt an extra ten points with the following code:db.profiles.findAndModify({ query: { name: "Matt", status: "active"}, update: { $inc: { score: 10 } } })
-
You get the following output (note that his score hasn’t increased yet):
{ "_id" : 1, "name" : "Matt", "status" : "active", "level" : 12, "score" : 202 }
-
You can verify that his score has changed with the following query:
db.profiles.find({name: "Matt"})
-
You get the following output:
{ "_id" : 1, "name" : "Matt", "status" : "active", "level" : 12, "score" : 212 }
Step 5: Explore
Congratulations! You have successfully completed the Get Started Guide to Amazon DocumentDB.
What’s next? Learn how to fully leverage this database with some of its popular features:
Note
The cluster you created from this get started exercise will continue to accrue cost unless you delete it. For directions, see Deleting an Amazon DocumentDB Cluster.