The popularity of cloud databases in the last few years has been undeniable, and it is well-utilized in various industries. The MongoDB NoSQL database in particular is a go-to among developers and companies alike. According to MongoDB revenue data from 2022, there were over 300,000 sign-ups for the Atlas free tier, which is an increase of over 15X in the last five years. MongoDB is used by customers who are moving to the cloud at scale, thanks to its incredibly useful Atlas offering.
fig. Image by rawpixel.com on Freepik
Why Choose MongoDB Atlas?
In building a database, it’s important to make sure your platform of choice is open-source and easily scalable. The ability to support complex queries and fine-grained concurrency control is also valued. Fortunately, Atlas offers all of these features and more. It allows you to create a MongoDB cluster on major cloud providers such as AWS, Azure, and Google Cloud platforms, and start using that cluster soon after deployment. Furthermore, Atlas’s browser-based UI makes its features easier to grasp, even for beginner coders.
Getting Started with MongoDB Atlas
Before the actual process, make sure that the latest version of Python and PIP are properly installed and configured on your device. After these steps, you can now proceed to learn how to create a MongoDB database.
How to Create a Database on Atlas
After downloading resources, create an account for MongoDB Atlas. To create one, simply go to the homepage and press the ‘Get started free’ button. Choose the cloud provider of your choice and an available region for your provider. However, make sure that you’re picking a region that is nearest to your location to reduce latency. Next, select your cluster tier. We recommend M0 or the free cluster tier. This shared cluster works great for small projects and learning purposes. You can then give your cluster a permanent name.
Configuring the Cluster
After MongoDB finishes processing your cluster, you will see that you can now access the ‘Security’ tab. You have the option to select your device’s IP address, or allow access from anywhere – the first option suffices. Autofill the Whitelist Entry box using the ‘Add current IP address’ button and confirm. Now switch to the ‘Overview’ tab and click on ‘Connect’. If you’ve followed our previous step and updated your driver accordingly, you’ll be able to see your connection string. Take note of the string, and input a password on the second field.
Obtaining Connection String
To interact with your MongoDB Atlas cluster, you should utilize the popular driver PyMongo. The PIP module installed earlier will be the easiest way to install it onto your device. To use it, input ‘python3 -m pip install pymongo --user’. You’ll also notice that your MongoDB Atlas connection string will show as ‘mongodb+srv://’. You need to install the dnspython module to allow the driver to function with DNS SRV records via the ‘python3 -m pip install dnspython’ command.
Managing Your Cluster
Now that we have PyMongo, you can proceed to interact with your MongoDB Cluster. The following will allow you to do so:
If there are any errors with the username or password you specified, you can use the following code to spot an operation failure error:
To test your database, use:
To create a new collection, you first have to add a document. The ‘insert_one()’ method is the simplest way to do so:
If you want to add or delete multiple documents, run complex queries, or find documents, you can refer to MongoDB’s named query operators.
Overall, MongoDB Atlas is an ingenious way to store and access data and a helpful tool that lets you focus on development without worrying about backend tasks such as security, maintenance, and scaling.