Quick Start: Getting Started With MongoDB Atlas and Python
Rate this quickstart
- How to set up MongoDB Atlas in the cloud
- How to load sample data
- How to query sample data using the PyMongo library
Sign up for Atlas and log into your account.
- You can choose any cloud instance.
- Choose the “FREE” tier.
- Follow the setup wizard and give your instance a name.
- Note your username and password to connect to the instance.
- Add 0.0.0.0/0 to the IP access list.
This makes the instance available from any IP address, which is okay for a test instance.
See the screenshot below for how to add the IP:
Next, let’s load the sample datasets available in Atlas by default. Note that loading the data may take a few minutes.
You can browse the data in the Atlas UI. Let’s look at the embedded_movies collection in the sample_mflix database. You will see that each document has details such as the title, year, plot, etc.
To connect to Atlas, we need the MongoDB connection string. Here's how you get it:
- Navigate to the Atlas UI.
- Select the database you want to connect to.
- Choose the Connect option to proceed.
- Within the connect section, click on Drivers to view connection details.
- Finally, copy the displayed connection string for use in your application's configuration.
See the below screenshots for guidance:
Once you get the connection string, assign it to a variable in your Python code:
We are keeping this very simple for the purpose of this quick start. For production systems, consider using libraries like python-dotenv to get configuration settings.
To connect to our Atlas cluster using the Pymongo client, we will need to install the following libraries:
We only need one package here:
- pymongo: Python library to connect to MongoDB Atlas.
This
AtlasClient
class will handle tasks like establishing connections, running queries, etc. It has the following methods:- init: Initializes an object of the AtlasClient class, with the MongoDB client (
mongodb_client
) and database name (database
) as attributes - ping: Used to test if we can connect to our Atlas cluster
- get_collection: The MongoDB collection to connect to
- find: Returns the results of a query; it takes the name of the collection (
collection
) to query and any search criteria (filter
) as arguments
In this phase, we will establish a connection to the embedded_movies collection within the sample_mflix database. To confirm that our connection is successful, we'll perform a
ping()
operation.If you get a “Connection failed” error, make sure 0.0.0.0/0 is added as an allowed IP address to connect (see Step 1).
Let's execute a search for movies using the
find()
method. The find()
method takes two parameters. The first parameter, collection_name
, determines the specific collection to be queried — in this case, embedded_movies. The second parameter, limit
, restricts the search to return only the specified number of results — in this case, 5.The results are returned as a list and we are simply iterating over it and printing out the results.
If we want to query by a certain attribute, we can pass a
filter
argument to the find()
method. filter
is a dictionary with key-value pairs. So to find movies from the year 1999, we set the filter as {"year" : 1999}
.We see that 81 movies are returned as the result. Let’s print out the first few.
In this quick start, we learned how to set up MongoDB Atlas in the cloud, loaded some sample data into our cluster, and queried the data using the Pymongo client. To build upon what you have learned in this quickstart, here are a few more resources: