Advertisement
  1. Code
  2. Cloud & Hosting

Create a Database Cluster in the Cloud With MongoDB Atlas

Scroll to top
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 AtlasHome page of MongoDB AtlasHome 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 selectionCloud provider and region selectionCloud 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 selectionCluster tier selectionCluster tier selection

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

Cluster name formCluster name formCluster 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 formAdd new user formAdd 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 formAdd whitelist entry formAdd 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 dialogConnecting your application dialogConnecting 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:

1
python3 -m pip install pymongo --user

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:

1
python3 -m pip install dnspython --user

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.

1
import pymongo
2
3
4
5
my_client = pymongo.MongoClient(
6
7
    'mongodb+srv://alice:myPassword@mylittlecluster-qsart.gcp.mongodb.net/test?retryWrites=true'
8
9
)

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:

1
try:
2
3
    print("MongoDB version is %s" % 
4
5
            my_client.server_info()['version'])
6
7
except pymongo.errors.OperationFailure as error:
8
9
    print(error)
10
11
    quit(1)

You can now go ahead and try running your script.

Python script outputPython script outputPython 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:

1
my_database = my_client.test

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.

1
my_collection = my_database.foods

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:

1
my_collection.insert_one({
2
3
    "_id": 1,
4
5
    "name": "pizza",
6
7
    "calories": 266,
8
9
    "fats": {
10
11
        "saturated": 4.5,
12
13
        "trans": 0.2
14
15
    },
16
17
    "protein": 11
18
19
})

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:

1
my_collection.insert_many([
2
3
    {
4
5
        "_id": 2,
6
7
        "name": "hamburger",
8
9
        "calories": 295, "protein": 17,
10
11
        "fats": { "saturated": 5.0, "trans": 0.8 },
12
13
    },
14
15
    {
16
17
        "_id": 3,
18
19
        "name": "taco",
20
21
        "calories": 226, "protein": 9,
22
23
        "fats": { "saturated": 4.4, "trans": 0.5 },
24
25
    }
26
27
])

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:

1
my_cursor = my_collection.find()
2
3
4
5
for item in my_cursor:
6
7
    print(item["name"])
8
9
10
11
# Output is:

12
13
#   pizza

14
15
#   hamburger

16
17
#   taco

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:

1
my_cursor = my_collection.find({
2
3
    "name": "pizza"
4
5
})

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:

1
my_cursor = my_collection.find({
2
3
    "calories": { "$lt": 280 }
4
5
})
6
7
8
9
for item in my_cursor:
10
11
    print("Name: %s, Calories: %d" % 
12
13
        (item["name"], item["calories"]))
14
15
16
17
# Output is:

18
19
#   Name: pizza, Calories: 266

20
21
#   Name: taco, Calories: 226

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:

1
my_cursor = my_collection.find({
2
3
    "fats.trans": { "$gte": 0.5 }
4
5
})
6
7
8
9
for item in my_cursor:
10
11
    print("Name: %s, Trans fats: %.2f" % 
12
13
        (item["name"], item["fats"]["trans"]))
14
15
16
17
# Output is:

18
19
#   Name: hamburger, Trans fats: 0.80

20
21
#   Name: taco, Trans fats: 0.50

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":

1
my_collection.update_one(
2
3
    { "name": "taco" }, # query

4
5
    {
6
7
        "$set": {       # new data

8
9
            "fiber": 3.95,
10
11
            "sugar": 0.9
12
13
        }
14
15
    }
16
17
)

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:

1
my_collection.delete_many({
2
3
    "calories": {
4
5
        "$lt": 300
6
7
    }
8
9
})
10
11
12
13
# Deletes all the three documents

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
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
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.