Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Advertisement
  1. Code
  2. MongoDB
Code

Create a Database Cluster in the Cloud With MongoDB Atlas

Difficulty:BeginnerLength:MediumLanguages:
Sponsored Content

This sponsored post features a product relevant to our readers while meeting our editorial guidelines for being objective and educational.

For years now, MongoDB has been the go-to NoSQL database for both individuals and enterprises building large-scale applications. It's open source, easily scalable, and provides high availability. It also supports very complex queries and fine-grained concurrency control. 

However, necessary tasks such as installing the database, tuning it to maintain optimal performance over long periods of time, and securing it tend to require a lot of skilled and dedicated effort. 

Fortunately, there's an easier alternative: MongoDB Atlas, a fully managed, cloud version of the database.

With MongoDB Atlas, you can create a MongoDB cluster on any major cloud provider of your choice and start using that cluster in a matter of minutes. Using Atlas's browser-based user interface, you can also intuitively configure the cluster and monitor its performance.

In this tutorial, I'll show you how to create a MongoDB Atlas free tier cluster and use it in a Python application.

Prerequisites

Before you proceed, make sure you have the following installed and configured on your computer:

1. Creating a Cluster

To be able to use MongoDB's cloud services, you'll need a MongoDB Atlas account. To create one, go to its home page and press the Get started free button.

Home page of MongoDB Atlas

After you complete the short sign-up form, you'll be redirected to the cluster creation wizard. In its first section, you'll have to pick the cloud provider and region you prefer.

To minimize network latency, you'd ideally pick a region that's nearest to your computer. For now, however, because we are creating a free tier cluster, make sure the region you select is one that has a free tier available. Additionally, if you are using a Google Cloud VM or an Amazon EC2 instance as your development environment, do select the corresponding cloud provider first.

Cloud provider and region selection

In the Cluster Tier section, select the M0 option to create your free tier cluster. It offers 512 MB of storage space, a recent version of MongoDB with WiredTiger as the storage engine, a replica set of three nodes, and a generous 10 GB of bandwidth per week.

Cluster tier selection

Lastly, give a meaningful name to the cluster and press the Create Cluster button.

Cluster name form

MongoDB Atlas will now take about five minutes to set up your cluster.

2. Configuring the Cluster

Before you start using the cluster, you'll have to provide a few security-related details, so switch to the Security tab.

First, in the MongoDB Users section, you must create a new user for yourself by pressing the Add new user button. In the dialog that pops up, type in your desired username and password, select the Read and write to any database privilege, and press the Add User button.

Add new user form

Next, in the IP Whitelist section, you must provide a list of IP addresses from which you'll be accessing the cluster. For now, providing the current IP address of your computer is sufficient.

Press the Add IP address button to create a new IP address entry. In the dialog that pops up, press the Add current IP address button to autofill the Whitelist Entry field. Additionally, if you don't have a static IP address, it's a good idea to mark it is a temporary entry by checking the Save as temporary whitelist option. Finally, press Confirm to add the entry.

Add whitelist entry form

3. Getting the Connection String

You'll need a valid connection string to connect to your cluster from your application. To get it, go to the Overview tab and press the Connect button.

In the dialog that opens, select the Connect Your Application option and press the I'm using driver 3.6 or later button. You should now be able to see your connection string. It won't have your actual password, so you'll have to put it in manually. After you do so, make a note of the string so you can use it later.

Connecting your application dialog

4. Installing the Python Driver

To be able to interact with your MongoDB Atlas cluster programmatically, you must have a MongoDB driver installed on your computer. For the Python programming language, PyMongo is the most popular driver available today. The recommended way to install it on your computer is to use the pip module as shown below:

You may have noticed that your MongoDB Atlas connection string is a mongodb+srv:// URI. To enable the driver to work with DNS SRV records, you must also install the dnspython module. Here's how:

5. Connecting to the Cluster

You can now use your MongoDB cluster from any Python application. To follow along with me, create a new Python script and open it using any code editor.

Inside the script, to be able to interact with the cluster, you'll need an instance of the MongoClient class. As the only argument to its constructor, pass your connection string.

The above constructor returns immediately and will not raise any errors. Therefore, to check if you've successfully established a connection, I suggest you try to perform an operation on the cluster. A call to the server_info() method, which gets you various details about your MongoDB instance, will suffice.

If there are no errors in your connection string, the call to the server_info() method will succeed. However, if the username or password you specified is incorrect, you'll encounter an OperationFailure error. The following code shows you how to catch it:

You can now go ahead and try running your script.

Python script output

6. Inserting Documents

The default connection string you get from MongoDB Atlas's web interface mentions a database named test. Let's continue to use the same database. Here's how you can get a reference to it:

A MongoDB database is composed of one or more collections, which are nothing but groups of BSON documents (short for binary JSON). Your free tier cluster on MongoDB Atlas can have a maximum of 500 collections.

For the sake of a realistic example, let's create a new collection named foods. With PyMongo, you don't have to explicitly call any method to do so. You can simply reference it as if it exists already.

It's worth mentioning that the above code doesn't create the foods collection immediately. It's created only after you add a document to it. So let's now create and add a new document containing nutritional data about a food item.

Using the insert_one() method is the simplest way to add a document to a collection. To specify the contents of the document, you pass a Python dictionary to the method. The following sample code shows you how:

Adding documents one at a time can be inefficient. With the insert_many() method, you can add several documents to your collection at once. It expects an array of dictionaries as its input. The following code adds two more documents to the collection:

The _id field you see in the above documents is a field that's used as a primary key by MongoDB. As such, it is immutable and must be present in all MongoDB documents. If you forget to include it while creating your document, PyMongo will add it for you automatically and assign an auto-generated unique value to it.

7. Running Queries

When you've added a few documents to your collection, you can run queries on it by calling the find() method, which returns a Cursor object you can iterate over. If you don't pass any arguments to it, find returns all the documents in the collection.

The following code shows you how to print the names of all the food items present in our collection:

If you want the find() method to return only those documents that match specific criteria, you must pass a Python dictionary to it. For example, if you want to find the document whose name field is set to "pizza", you could use the following code:

For more complex queries, you can use MongoDB's intuitively named query operators in the dictionaries you pass to the find() method. For instance, the following code shows you how to use the $lt operator to find documents whose calories field is set to a value that's less than 280:

By using the dot notation, you can also use nested fields in your queries. The following code shows you how to find documents whose trans field, which is inside the fats field, is set to a value that's greater than or equal to 0.5:

8. Updating and Deleting Documents

Very similar to the insert_one() and insert_many() methods are the update_one() and update_many() methods, which you can use to change the contents of documents that are already inside your collection. Both the update methods, in addition to new data, need a query to zero in on the documents that need to be changed.

You can use a variety of update operators in your update methods. The most commonly used one is $set, which lets you add new fields or update the values of existing fields. The following code shows you how to add two new fields named fiber and sugar to the document whose name field is set to "taco":

If the query you pass to the update_one() method returns more than one document, only the first document is updated. The update_many() method doesn't have this limitation.

Lastly, by using the delete_one() and delete_many() methods, you can delete documents in your collections. Both the methods need a query to determine which documents need to be deleted. Here's how you can delete all documents whose calories field is set to a value that's less than 300:

Conclusion

If you are a web or mobile application developer who wants to use MongoDB for storing data, the MongoDB Atlas service is for you. It lets you focus on developing your application instead of worrying about details such as security, performance, and adherence to best practices. In this tutorial, you learned how to create a MongoDB cluster using the service, connect to it, and perform basic read and write operations on it.

To learn more about MongoDB Atlas, you can refer to the official documentation.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.