Getting Started Wth MongoDb Atlas

Introduction

In this article, we will learn how to get started with MongoDB Atlas. MongoDB Atlas is the global cloud database service for the modern application. MongoDB Atlas is the best way to deploy, run, and scale MongoDB In the Cloud.

Let's begin...

First of all, visit https://www.mongodb.com/cloud/atlas URL in order to sign up for the MongoDB Atlas Account.

MongoDB Atlas Account

Log in

The next step is to add an organization. Within the organization, we can create projects, invite users, and can set up a billing account as well.

Billing account

Give a name to your organization and select the MongoDB Atlas as a Cloud Service. Click on Next.

Cloud Service

Click on the Create Organization button in order to create an organization.

Create an organization

Click on the New Project button in order to add the project to the Organization.

New Project button

Provide a Name to your Project. The project name must be unique within an organization. Click on the Next button.

Next button

Click on Create Project in order to add the Project to the organization.

Create Project

The next step is to Add a Cluster to the Project. Click on Build a Cluster in order to create a Cluster.

Build a Cluster

Select the Cluster as per your requirement. I am choosing a shared cluster as I am using it for demonstration purposes.

Demonstration purposes

Shared Cluster is for Teams learning MongoDB or developing small applications. Click on the Create a Cluster button, as shown in the below image.

 Team learning MongoDB

Select a starter Cluster in Shared Cluster. Pick the Cloud Provider and Region where you want to create a Cluster. Click on the Create a Cluster button.

Cloud Provider

It will take around 3 minutes to set up a cluster in the cloud. You can also add another cluster by clicking on the Add a new Cluster button (if needed).

Cluster button

Once the cluster is ready, the Connect, Metrics, and Collections button will be enabled.

Collections button

Click on the Network Access menu and then Add click on the IP Address button as Atlas only allows client connections to a cluster from entries in a Project’s IP Address list.

 IP Address button

For Demonstration, I am allowing Access from Anywhere in the IP Address Access List (as we will access the same cluster through the .NET Core Application). Click on Confirm in order to proceed.

 IP Address Access List

Once added, you can see the whitelist IP Address in the grid. You can edit or delete any allowed IP Address at any time.

IP Address in the grid

Now click on the Database access menu in order to create a Database user, set permission, and authentication credentials to connect with a cluster. Click on Add new database user.

Database access menu

Select the Password as the Authentication method, type or auto-generate a secure password, and give read and write to any database. Click on Add user.

Authentication method

User database

All of the added users are visible in the grid in the Database user tab.

Database user tab

Click on three dots (options) in Sandbox Cluster, and Click on Load Sample Dataset. A sample dataset which is of around 350 MB in size will be loaded in the cluster.

Sandbox Cluster

Load sample dataset

The sample dataset will be loaded in a few minutes.

Sample dataset

Our Dataset is ready. Now let’s create a .NET Core Console App through which we will try to access one of the collections in the MongoDB Atlas cluster.

MongoDB Atlas cluster

Give a name to your .NET Core project. Click on the Create button.

.NET Core project

Once the solution is ready, right-click on the project’s dependencies and select Manage NuGet Packages.

Project’s dependencies

Now install MongoDB Driver from the NuGet package manager as shown in the image below. Click on the Install button, in order to install the package.

MongoDB Driver

In order to connect the C# .Net Core Application with the MongoDB Atlas’s Cluster, we need a connection string. Click on the connect button of the Cluster created.

Net Core Application

A popup will appear from where we have to choose a connection method. Click on the Connect Your Application link as shown in the below image.

Connect your application

Select the Driver and the Version of the Driver (installed through NuGet package manager) which we are using to connect with MongoDB through the C# .NET Core console application. Click on the copy button in order to copy the connection string. Change <password> to the password added for the database user and <dbname> to the database name with which you want to connect.

NuGet package manager

There are multiple databases in the Sample Dataset loaded. We will use the sample_mflix database in the connection string.

Sample Dataset loaded

Now add the below C# Code in the main method of the .NET Core Console Application.

// Passing the connection string in MongoClient
var client = new MongoClient("mongodb+srv://TestUser:[email protected]/sample?retryWrites=true&w=majority");

// Get sample_mflix Database
var db = client.GetDatabase("sample_mflix");

// Get movies collection
var collection = db.GetCollection<BsonDocument>("movies");

// Find document with title 'Peter Pan'
var result = collection.Find("{title:'Peter Pan'}").FirstOrDefault();

Console.WriteLine(result);

Preview

Preview

On running the application, a document with the Title “Peter Pan” will be printed on the console.

I hope this will help you get started with MongoDB Atlas.


Similar Articles