MongoDB - Getting StartedMongoDB - Getting Started1

Creating and Managing Databases in Atlas



Creating and Managing Databases in Atlas

MongoDB Atlas is the official cloud service provided by MongoDB Inc. It allows developers to run, manage, and scale MongoDB clusters in the cloud — with no need to manually install MongoDB on a server.

In this tutorial, you will learn how to create a MongoDB database in Atlas, access it using MongoDB Compass, and manage your collections and documents from the cloud.

Step 1: Sign Up for MongoDB Atlas

Visit mongodb.com/cloud/atlas and create a free account. Choose the Free Shared Cluster (M0) tier during setup — it’s enough for learning and small projects.

Step 2: Create a Cluster

Once signed in:

This takes a couple of minutes to provision.

Step 3: Create a Database and Collection

Once your cluster is ready, click on “Browse Collections” and then:

Let’s add this document:


    {
      "name": "Alice",
      "email": "alice@example.com",
      "age": 28
    }
    

Output:

    {
      _id: ObjectId("..."),
      name: "Alice",
      email: "alice@example.com",
      age: 28
    }
    

Explanation: You’ve just created a database, a collection, and inserted your first document — all from the browser interface of MongoDB Atlas.

Step 4: Add Database User and IP Whitelist

To connect from your code or Compass, you must:

Step 5: Connect Using Compass or Code

Click “Connect” and choose “Connect using MongoDB Compass”. Copy the connection string and paste it in Compass.


    mongodb+srv://mongoUser:<password>@cluster0.xxxxx.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
    

Replace <password> with your database password and click “Connect”. You should now see your database and the users collection with the inserted document.

Connecting Using Code (Node.js)

Install MongoDB driver in your Node.js project:


    npm install mongodb
    

Then use the following code:


    const { MongoClient } = require('mongodb');

    const uri = 'mongodb+srv://mongoUser:<password>@cluster0.xxxxx.mongodb.net/myFirstDatabase?retryWrites=true&w=majority';

    async function run() {
      const client = new MongoClient(uri);
      try {
        await client.connect();
        const database = client.db("myFirstDatabase");
        const collection = database.collection("users");

        const user = { name: "Bob", email: "bob@example.com", age: 32 };
        const result = await collection.insertOne(user);
        console.log("Inserted:", result.insertedId);
      } finally {
        await client.close();
      }
    }

    run().catch(console.dir);
    

Output:

    Inserted: 651e72f5e60adf003abc1234
    

Explanation: This Node.js script connects to your MongoDB Atlas cluster and inserts a document into the users collection.

Question

Q: What if I forget to whitelist my IP address?

A: You’ll get a network timeout error when trying to connect. Always make sure your current IP is whitelisted in the “Network Access” settings in Atlas.

Managing Collections

From the Atlas UI, you can:

Summary

MongoDB Atlas makes it incredibly easy to create and manage databases in the cloud. You can insert documents, connect from applications, and manage everything via a secure and user-friendly dashboard.

In the next topic, you’ll learn how to connect your Atlas database to real-world applications using Python and Node.js drivers.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

Mention your name, and programguru.org in the message. Your name shall be displayed in the sponsers list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M