Hero Image

How to Connect to AWS DocumentDB Using MongoDB

 

13 Mar 2025 | Emma Benham

scroll down next block

How to Connect to AWS DocumentDB Using MongoDB Compass

AWS DocumentDB is a managed NoSQL database service that supports MongoDB workloads. MongoDB Compass is a graphical tool that allows developers to interact with MongoDB-compatible databases, such as AWS DocumentDB. This guide will walk you through connecting AWS DocumentDB to MongoDB Compass.

Connect to AWS DocumentDB Using MongoDB Compass with gravity9

Prerequisites

Before proceeding, ensure you have the following:

  1. AWS DocumentDB Cluster:
    • A running DocumentDB instance in your AWS account.
    • The cluster should be configured for access.
  2. MongoDB Compass:
  3. Connection String:
    • Retrieve the connection string for your DocumentDB cluster from the AWS Console.
  4. Network Access:
    • Security groups configured to allow inbound connections.
    • Your local machine’s IP should be whitelisted in the AWS security group.

Retrieve Connection Details

  1. Log in to AWS:
  2. Find the Connection String:
    • On the cluster details page, locate the connection string in the Connectivity & Security section.

The string will look something like this:

ruby

mongodb://username:password@your-cluster-endpoint:27017/?ssl=true&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false

  • Note: Replace username and password with your actual credentials.

Configure MongoDB Compass

  1. Open MongoDB Compass:
    • Launch the MongoDB Compass application on your computer.
  2. Set Up a New Connection:
    • In the Compass connection form, choose “Connect with Connection String”.
  3. Paste the Connection String:
    • Copy the connection string retrieved earlier.
    • Replace <your_password> with your cluster password. Ensure no special characters in the password conflict with the URL syntax (e.g., @ or /).

Adjust Network Settings

To successfully connect, your local machine must be able to reach the AWS DocumentDB cluster:

  1. Security Group Configuration:
    • In the AWS Console, go to the Security Groups section under VPC.
    • Edit the inbound rules to allow traffic from your local machine’s IP:
      • Protocol: TCP
      • Port Range: 27017
      • Source: Your Local IP (e.g., 203.0.113.0/32).
  2. Test Connectivity:
    • Use a tool like ping or telnet to confirm the endpoint is reachable.

Test the Connection in MongoDB Compass

  1. Enable SSL:
    • Ensure the connection string includes ?ssl=true to enable SSL/TLS encryption.
    • If your configuration requires custom certificates, add the certificate path in Compass under Advanced Options.
  2. Click “Connect”:
    • MongoDB Compass will attempt to connect to your DocumentDB cluster.
    • Once successful, you’ll see the database collections listed in the left-hand panel.

Troubleshooting Common Issues

  1. Authentication Errors:
    • Double-check the username and password used in the connection string.
    • Ensure the user has the necessary privileges in DocumentDB.
  2. Network Issues:
    • Confirm your local machine’s IP is added to the security group.
    • Verify that the cluster endpoint is accessible from your network.
  3. Compass Compatibility:
    • Ensure you’re using a version of MongoDB Compass that supports AWS DocumentDB. DocumentDB supports MongoDB 3.6, 4.0, and 5.0 APIs.

Verify and Explore

Once connected:

  • Use MongoDB Compass to browse databases, collections, and documents.
  • Perform basic CRUD operations (Create, Read, Update, Delete).
  • Export and import data for additional testing.

Best Practices

  1. Secure Connections:
    • Always use SSL/TLS to secure data in transit.
    • Rotate passwords periodically.
  2. Manage Costs:
    • Shut down idle clusters to avoid unnecessary costs in AWS.
  3. Monitor Performance:
    • Use AWS CloudWatch for monitoring metrics related to your DocumentDB cluster.

FAQs

  1. Can I connect to AWS DocumentDB from a public network?
    • Yes, but you need to configure the cluster’s security group to allow access from your IP. Public access should be avoided unless necessary.
  2. Does MongoDB Compass support all DocumentDB features?
    • While Compass supports basic operations, some advanced MongoDB features may not be fully compatible with DocumentDB.
  3. What ports need to be open for DocumentDB?
    • Ensure port 27017 is open in your security group for inbound traffic.

By following this guide, you can seamlessly connect AWS DocumentDB with MongoDB Compass, enabling efficient data management and analysis. Contact us For further assistance or to streamline your cloud and database strategies, partner with gravity9—your trusted legacy data modernisation experts.