BlogAnnounced at MongoDB.local NYC 2024: A recap of all announcements and updatesLearn more >>
MongoDB Developer
Atlas
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Atlaschevron-right

Quick Start: Getting Started With MongoDB Atlas and Python

SM
Sujee Maniyam4 min read • Published Apr 09, 2024 • Updated Apr 10, 2024
AtlasPython
Facebook Icontwitter iconlinkedin icon
Rate this quickstart
star-empty
star-empty
star-empty
star-empty
star-empty

What you will learn

  • How to set up MongoDB Atlas in the cloud
  • How to load sample data
  • How to query sample data using the PyMongo library

Where's the code?

The Jupyter Notebook for this quickstart tutorial can be found here.

Step 1: Set up MongoDB Atlas

Here is a quick guide adopted from the official documentation:

Create a free Atlas account

Sign up for Atlas and log into your account.

Create a free instance

  • 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:
Editing Network Access settings in the MongoDB Atlas UI

Load sample data

Next, let’s load the sample datasets available in Atlas by default. Note that loading the data may take a few minutes.
Loading sample datasets in the MongoDB Atlas UI

View sample data

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.
Viewing sample data in the MongoDB Atlas UI

Step 2: Setup prerequisites

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:
enter image description here
Obtaining the connection string from the MongoDB Atlas UI
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.

Step 3: Install the required libraries

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.

Step 4: Define the AtlasClient class

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

Step 5: Connect to MongoDB Atlas

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).

Step 6: Run a sample query

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.

Query by an attribute

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.

Conclusion

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:

Facebook Icontwitter iconlinkedin icon
Rate this quickstart
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Article

Optimizing your Online Archive for Query Performance


Jan 23, 2024 | 3 min read
Podcast

MongoDB Atlas Multicloud Clusters


May 16, 2022 | 25 min
Tutorial

Searching on Your Location with Atlas Search and Geospatial Operators


Feb 03, 2023 | 9 min read
Quickstart

Single Click to Success: Deploying on Netlify, Vercel, Heroku, and Render with Atlas


Apr 10, 2024 | 6 min read
Table of Contents