How to Create a MongoDB Cluster in MongoDB Atlas
MongoDB Atlas is a fully managed cloud database service built and maintained by the same team behind MongoDB. In this lesson, we'll create our first MongoDB cluster with MongoDB Atlas.
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL, plus 70+ \newline books, guides and courses with the \newline Pro subscription.
[00:00 - 00:13] Just like how we intend to deploy our server to the cloud, we're going to implement our database on the cloud as well. This is important because the moment our servers in the cloud during production , it needs to access a database on the cloud as well.
[00:14 - 00:28] We can't just have a database on our static local computer. In this lesson, we're going to set up a Mongo cluster with MongoDB Atlas, which is a fully managed cloud database built and maintained by the same team behind MongoDB.
[00:29 - 00:45] Now, a cluster is essentially a group of two or more servers that work together to provide users with higher availability, scalability, and reliability. To use Mongo's cloud service, Mongo Atlas, the first thing that needs to be done is to have an account created.
[00:46 - 00:57] We're going to go through the account creation process here again, just to show you the steps that are needed to be done to create an account, and finally create a new cluster. We'll go to the login, register page.
[00:58 - 01:10] Now, as a side note, we're going to provide this link and all the other additional links that might be needed in the lesson documentation. In the register page, this is where we can fill our information to create a brand new account.
[01:11 - 01:39] Once we've submitted the form and created our account, we will be redirected and prompted to create our very first cluster. Here's where MongoDB Atlas recommends a lot of their popular options, but it do give us the capability of customizing how our cluster is set up the way we see fit.
[01:40 - 01:54] The first thing that they actually recommend or discuss is the cloud provider and region for where our cluster is going to be. In terms of cloud providers, they offer AWS, Google Cloud Platform, Azure, and in terms of regions, there's North America, Europe, and even Asia.
[01:55 - 02:09] Now, there is some differences in terms of using a free tier or not. We're going to stick with a free tier, and they also recommend so, and we advise you to only think about upgrading to a production level cluster, only when you're ready to actually move to a production level app.
[02:10 - 02:17] But for the other than that, we're going to keep things very simple. We're not actually going to change any of the settings they provide, and we're simply going to just create our cluster as is.
[02:18 - 02:34] In terms of region, it's going to be kept in North Virginia, US East One. Note that it takes about seven to ten minutes to create and provision a new cluster.
[02:35 - 02:49] So to actually continue, we'll have to wait just a few minutes until a cluster is going to be created. Once ready, your cluster is being created, the message will no longer be seen, and we'll actually see some information about our cluster.
[02:50 - 03:06] Notice that Atlas actually creates our cluster within a particular project. Atlas allows us to create a hierarchy of organizations and projects to manage many different clusters if we may have them, but in this case, we already have a project created for us, and we have a cluster now created within our project.
[03:07 - 03:23] There's a few things we're now going to do to get started, the first being creating a new database user. On the left-hand side panel, we'll click the database access button in the security section, and then we're going to click add new user.
[03:24 - 03:43] This new user will have read and write access to the database cluster, so we'll fill in the user information, and then finally we're going to add this new user. The next thing we're going to do is we're going to whitelist an IP address.
[03:44 - 03:57] We'll head to the network access section under security of the side nav panel, and then we'll click add IP address. Here we have the option to choose which IP addresses can have access to our database.
[03:58 - 04:15] To keep things simple, and since we're going to host our server to the cloud, which doesn't really have a predetermined IP address, we'll simply click allow access from anywhere. Next, we're going to add some sample data into our cluster.
[04:16 - 04:29] We'll go back to the clusters dashboard, where we're actually able to see information about our cluster, and we'll click collections. Here we'll then click add my own data, and we'll provide a name for our database.
[04:30 - 04:55] We'll call it main to reference it as the main database, and then we'll give a name to our collection, with which we can say test_list ings to reference it as the sample test collection. One thing that we really like about MongoDB Atlas is their really intuitive UI to be able to sort of navigate and see the data within a database and collections.
[04:56 - 05:07] In this case, we're actually able to see information within the main database we've created and the test_listings collection, as specified on the right-hand side as main. test_listings.
[05:08 - 05:18] At this moment, we don't have any data, but if we did, we'd also be able to sort of find and query information right in our UI right here. With that said, let's actually add some new data.
[05:19 - 05:36] We'll click the insert document button on the top right corner, and in this case, we're able to insert a new document into our collection. We'll keep it simple, and we're going to add two new fields here, just title and address, that denotes a simple document that represents a single listing.
[05:37 - 05:54] You're more than welcome to add what you like, but we're going to fill this in with some mock data of our own. Notice that as we're inserting fields here, we're actually able to insert the type of field you want.
[05:55 - 06:08] That Mongo actually recognize things like null integer, 64-bit integer, 32-bit integer, code, boolean, binary, arrays, etc. But in our case, the two fields we're going to add are just going to be strings .
[06:09 - 06:25] The object ID field is unique because every single document within a collection has a unique identifier, and this is the unique identifier that MongoDB Atlas has already prepared for us. Once inserted, we're able to see in the UI the documents we've just added.
[06:26 - 06:42] With sample data available in our cluster, the only thing left for us to do is to connect our server application with our MongoDB Atlas cluster. When we head over to the overview tab here, there's a connect button, which provides a series of different methods and options to actually make a connection.
[06:43 - 06:48] We're going to pick this up in the next lesson once we start to discuss how we 're going to make the connection from the server side.