Introduction to MongoDB and MongoDB Atlas (Part II)

Introduction to MongoDB and MongoDB Atlas (Part II)

In this article, we’re going to dive headfirst into the fascinating universe of MongoDB and MongoDB Atlas. No prior experience required — just your curiosity and a willingness to learn. Let’s embark on this journey together!

Understanding MongoDB: A Fresh Perspective

First things first, MongoDB is not your run-of-the-mill relational database. It’s a document-oriented database that operates on a unique premise. Instead of rows and columns, MongoDB stores data in JSON-like documents. This approach brings flexibility to the forefront while preserving the reliability and performance you’d expect from a database.

Think of MongoDB as a departure from the norm. It doesn’t organize data in traditional rows; it uses documents. These documents can nest sub-documents and arrays, allowing you to keep related data together, simplifying your database structure. MongoDB recognizes that the conventional third normal form doesn’t fit every use case. It’s optimized for availability, usability, scalability, and speed. To dive deeper into MongoDB document and database structure, consider reviewing the earlier sections of this training: Part I

When you interact with MongoDB, you’ll use idiomatic development drivers. These are libraries that blend seamlessly with your preferred programming language, making it easy to access MongoDB’s APIs and objects.

Meet MongoDB Atlas: Your Database Managed

Now, let’s turn our attention to MongoDB Atlas. Rather than setting up and managing MongoDB servers yourself, MongoDB Atlas takes care of it for you. MongoDB Inc. handles the nitty-gritty details, whether you want to host it on Amazon Web Services (AWS), Google Cloud, or other cloud providers. They manage installations, upgrades, and maintenance, leaving you free to concentrate on your application.

Today, we’re going to walk you through setting up your MongoDB Atlas cluster, securing it, establishing a connection, and offering a brief glimpse into the MongoDB shell.

Setting Up Your MongoDB Atlas Free Tier Cluster

Let’s begin by setting up a MongoDB Atlas free-tier cluster. This cluster is perfect for those eager to explore MongoDB without the hassle of server management. MongoDB Atlas ensures high availability across multiple data centers. If one data center faces issues, your data remains safe and accessible in another.

Here’s how to set up your MongoDB Atlas cluster:

  1. Visit cloud.mongodb.com.
  2. Sign up using your Google account or fill out the form.
  3. Once you’re in, accept terms and conditions.
  4. After login an organization and a project will be created for you. The organization represents your group or company, while the project is where your databases reside. You can change names that make sense for your use case.
Terms and Conditions

Selecting Your Cluster Configuration

Next, you’ll be prompted to choose your cluster configuration:

  • Dedicated clusters: Think of these as virtual machines exclusively for your use.
  • Shared tier clusters: Here, you share infrastructure, but your data remains isolated.
  • Multi-cloud clusters: These can span multiple cloud providers or regions.

For our training purposes, we’ll opt for the simplest option: a shared cluster. Click “Shared Clusters/M0” to proceed.

Picking a Region

Now, select a region near your location to ensure low latency and faster data access. Click “Create Cluster” to initiate the cluster setup.

Configuring Firewall and Creating a User

Before you connect to the cluster, you need to configure the firewall and create a user for authentication.

  • Firewall configuration: You can add your IP address or allow connections from anywhere. Click on “Add my current IP address” to allow access from your local computer.
  • User creation: Establish an initial administrative user. For training purposes, a simple password like “pass123” will suffice. In real-world scenarios, opt for secure passwords.
Security Quickstart

Cluster Initialization

Cluster initialization will take a few minutes. While it’s in progress, you can take a short break, chat with your fellow learners, or pose any questions you might have. Typically, initializing a cluster takes about 5 to 7 minutes.

Cluster Initialization

Connecting to Your MongoDB Atlas Cluster

Now, it’s time to connect to your MongoDB Atlas cluster using the MongoDB Shell. If you’d like to install the MongoDB shell on your local machine for more convenient use, follow these steps based on your operating system:

For Windows:

  1. Visit the MongoDB Download Center for Windows at https://www.mongodb.com/try/download/shell.
  2. Scroll down to the “MongoDB Shell” section and click on the “Download” button.
  3. Follow the installation wizard instructions, and MongoDB Shell will be installed on your Windows machine.

For macOS:

  1. Open your terminal.
  2. Use the Homebrew package manager to install MongoDB Shell. If you don’t have Homebrew installed, you can get it from https://brew.sh/.
  3. Run the following command to install MongoDB Shell:
brew tap mongodb/brew
brew install mongodb-community-shell

4. Once the installation is complete, you can access the MongoDB Shell by typing mongo in your terminal.

For Linux (Ubuntu/Debian):

  1. Open your terminal.
  2. Import the MongoDB public GPG Key to your system:
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

3. Create a MongoDB source list file:

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

4. Update the package list:

sudo apt-get update

5. Install MongoDB Shell:

sudo apt-get install -y mongodb-org-shell

6. Once the installation is complete, you can access the MongoDB Shell by typing mongo in your terminal.

With the MongoDB Shell installed, you’ll have a powerful tool to interact with MongoDB databases and collections from your local machine. This can be especially useful for local development and testing.

Connecting to Your MongoDB Atlas Cluster

  1. Access Cluster Overview: Click on the name of your selected cluster or use the “Connect” button associated with it. This will take you to the Cluster Overview page.
  2. Connect via the MongoDB Shell: On the Cluster Overview page, you’ll find several connection methods. Look for the one that says “Connect with the Mongo Shell” and click on it.
  3. Generate the Connection String: MongoDB Atlas will generate a connection string for you. This string contains all the necessary information to connect to your cluster, including the username, password, cluster details, and authentication database. Click the “Copy” button to copy the connection string to your clipboard.
  4. Use the Connection String: You can now paste the copied connection string into your local MongoDB Shell or any other MongoDB client you’re using. Open your shell and paste the connection string to establish a connection to your MongoDB Atlas cluster.

That’s it! You’re now ready to connect to your MongoDB Atlas cluster using the MongoDB Shell with the generated connection string. Make sure to keep your credentials and connection string secure, as they contain sensitive information for accessing your database.

Exploring MongoDB with the MongoDB Shell

With a successful connection, you can start exploring MongoDB using the MongoDB Shell. MongoDB organizes data into databases, collections, and documents. Here are some essential commands to help you get started:

  • show dbs: Lists available databases.
  • use <database>: Switches to a specific database or creates it if it doesn't exist.
  • show collections: Displays collections within the current database.
  • db.collection.find({}): Retrieves documents from a collection. An empty query, {}, fetches all documents.