Introducing Dragonfly Cloud! Learn More

Question: How do you create and connect to a MongoDB Atlas cluster?


Creating and connecting to a MongoDB Atlas cluster involves several steps. MongoDB Atlas is a fully-managed cloud database developed by the same people that build MongoDB. It automates the deployment, provides high availability, and secures your database with minimal setup. Here's a comprehensive guide:

Step 1: Sign Up for MongoDB Atlas

First, create an account on MongoDB Atlas. If you already have an account, simply log in.

Step 2: Create a Cluster

After logging in, you can create a new cluster by following these steps:

  • Click the 'Build a Cluster' button.
  • Choose a provider (AWS, Google Cloud Platform, or Microsoft Azure) and a region.
  • Select the Cluster Tier. The M0 tier (free tier) is suitable for learning and small projects.
  • Configure additional options if necessary, then click 'Create Cluster'.

Step 3: Configure Security Settings

For security, you need to set up network access and database access:

  • Network Access: Click 'Network Access' under the 'Security' tab. Add an IP address to allow connections from your current IP or anywhere (not recommended for production).
  • Database Access: Click 'Database Access' under the 'Security' tab. Add a new database user with a username and password. Remember these credentials as you'll need them to connect to your cluster.

Step 4: Connect to Your Cluster

Once your cluster is deployed, you can connect to it:

  • Click 'Connect' on your cluster's card.
  • Choose 'Connect your application'.
  • Select your driver and version. For most applications, you'll use the MongoDB native drivers.
  • You will be given a connection string. Replace <password> with the password of the database user you created earlier.

Example: Connecting Using MongoDB Node.js Driver

Here is a basic example of how to connect to your MongoDB Atlas cluster using the MongoDB Node.js driver:

const { MongoClient } = require('mongodb'); // Replace the following with your Atlas connection string const uri = 'mongodb+srv://your-user:your-password@your-cluster-url/test?retryWrites=true&w=majority'; const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); async function run() { try { await client.connect(); console.log('Connected to MongoDB Atlas'); // Perform actions on the collection object } finally { await client.close(); } } run().catch(console.dir);

Replace your-user, your-password, and your-cluster-url with your actual MongoDB Atlas credentials and cluster URL.


After following these steps, your MongoDB Atlas cluster will be set up and securely accessible from your application. MongoDB Atlas also offers features like monitoring, backups, and more to manage your databases efficiently.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.