DEV Community

Sam Purkayet
Sam Purkayet

Posted on

Creating a Cluster on MongoDB Atlas

Dedicated to @santhoshnc

MongoDB Atlas is a fully managed cloud database service that simplifies the deployment, management, and scaling of MongoDB clusters. Whether you're building a small prototype or a production-grade application, Atlas provides a user-friendly interface and robust features to get you started quickly. This polished guide walks you through creating a cluster on MongoDB Atlas, with detailed steps and practical tips to ensure a smooth setup.

Prerequisites

Before you begin, ensure you have:

  • A MongoDB Atlas account (sign up at mongodb.com).
  • A valid email address and, optionally, a payment method for paid tiers (a free tier is available for testing).
  • Basic familiarity with MongoDB concepts, such as collections and documents.

Step-by-Step Guide

1. Log in to MongoDB Atlas

Navigate to the MongoDB Atlas website and sign in with your credentials. If you’re new to Atlas, click Sign Up, provide your email, and follow the prompts to create an account. Verify your email to activate your account.

Login Page

2. Create a New Project

Upon logging in, you’ll arrive at the Atlas dashboard. Click New Project in the top-left corner. Enter a descriptive project name, such as "MyFirstCluster," to organize your databases. Click Next to proceed. Optionally, invite team members to collaborate or click Create Project to continue solo.

Create New Project

Project Name Input

3. Build a Database

In the project dashboard, locate and click Build a Database (or Create a Cluster). Atlas offers three cluster types:

  • Shared: Free tier, perfect for learning or small projects.
  • Dedicated: For production workloads with customizable resources.
  • Serverless: Pay-per-use for flexible scaling.

For this guide, select the Shared (free) tier and click Create. This tier, also known as the M0 Sandbox, provides 512 MB of storage, ideal for experimentation.

Cluster Tier Selection

4. Configure Cluster Settings

Customize your cluster with the following settings:

  • Cloud Provider & Region: Select a provider (AWS, Google Cloud, or Azure) and a region close to your target audience to minimize latency. For example, choose AWS’s us-east-1 for North American users.
  • Cluster Tier: Confirm M0 Sandbox for the free tier.
  • Cluster Name: Assign a name like "TestCluster" for easy identification.
  • Additional Settings: Retain defaults for MongoDB version and backup options, as backups are unavailable in the free tier.

Review the settings and click Create Deployment. Cluster creation typically takes 3–5 minutes.

Cluster Configuration

5. Create a Database User

While the cluster is provisioning, set up access. Click Database Access in the left menu, then Add New Database User in the pop-up. Create a username (e.g., "dbUser") and a strong password. Assign the Read and Write to any database role for full access. Save the credentials securely, as they’re needed for connecting to the cluster.

Database User Creation

6. Configure Network Access

To allow connections to your cluster, click Network Access in the left menu, then Add IP Address. For testing, select Allow Access from Anywhere (0.0.0.0/0). For production, specify your application’s IP or use VPC peering for enhanced security. Save the settings.

Network Access Setup

7. Choose a Connection Method

Once the cluster is ready, click Connect on the cluster dashboard. Atlas offers several connection options:

  • MongoDB Shell: Copy the connection string for use in the MongoDB shell.
  • Application: Select your programming language (e.g., Node.js, Python) and use the provided code snippet.
  • MongoDB Compass: Download and install MongoDB Compass, then paste the connection string.

Replace <password> in the connection string with your database user’s password. Test the connection to ensure it works.

Connection Options

8. Add a New Database to Your Cluster

Navigate to the Collections tab in your cluster dashboard and click Create Database. Enter a database name (e.g., "myDatabase") and a collection name (e.g., "myCollection"). This creates a storage structure for your data.

Database Creation

9. Insert a New Document

In the Collections tab, select your database and collection, then click Insert Document. Add a sample document manually or use the following JSON format:

{
  "_id": {
    "$oid": "68e36dcb2a6c4114653f4f29"
  },
  "name": "Saumyajit"
}
Enter fullscreen mode Exit fullscreen mode

Click Insert to save the document. Verify it appears in the collection.

Insert Document

Document View

10. Test Your Setup

To confirm everything is working, connect via your preferred method (e.g., MongoDB Compass or a code snippet) and run a query. For example, in the MongoDB shell or a Node.js application, insert and retrieve a document:

db.myCollection.insertOne({ name: "Test", date: new Date() });
db.myCollection.findOne();
Enter fullscreen mode Exit fullscreen mode

If the document is returned, your cluster is fully operational.

Best Practices

  • Security: Restrict network access to specific IPs in production to prevent unauthorized access.
  • Monitoring: Use Atlas’s built-in monitoring tools to track performance metrics like CPU usage and query latency.
  • Backups: For critical applications, upgrade to a paid tier to enable automated backups and point-in-time recovery.
  • Scaling: Monitor storage and performance needs. Upgrade to a higher tier (e.g., M10 or above) for production workloads.

Conclusion

Setting up a MongoDB Atlas cluster is a quick and intuitive process. Thanks to @santhoshnc for introducing to this world of NoSQL Databases.

For more information, check the MongoDB Atlas documentation.

Top comments (0)