Featured Article

Master Pyspark MongoDB Atlas Integration A to Z Guide

Kenneth Jul 13, 2026

Integrating Apache Spark with MongoDB Atlas can unleash unprecedented power in data processing and analytics. Both technologies, when harnessed together, offer a robust platform for handling big data, enabling real-time processing, and providing advanced analytics.

a drawing of a man holding the earth on his shoulders
a drawing of a man holding the earth on his shoulders

Spark's lightning-fast in-memory computing capabilities complement MongoDB's flexible document model, allowing you to process and analyze large datasets quickly and efficiently. This combination empowers data scientists and engineers to extract valuable insights from their data, fueling data-driven decision making.

an image of a statue that looks like it is holding the world in his hands
an image of a statue that looks like it is holding the world in his hands

Setting Up the Spark-MongoDB Atlas Connection

Establishing a connection between Spark and MongoDB Atlas involves a few straightforward steps. First, you need to set up an Atlas cluster and create a database user with necessary privileges. Then, you can use the MongoDB Spark Connector to read and write data to MongoDB from your Spark application.

a man carrying the world on his back with one arm and two hands in front of him
a man carrying the world on his back with one arm and two hands in front of him

Here's a simple example in PySpark:

Adding the MongoDB Connector

an old map with a man holding the world on it's shoulders and hands
an old map with a man holding the world on it's shoulders and hands

The first step is to ensure that the MongoDB Spark Connector is in your project's classpath. You can add it using Maven or SBT. For Maven, include this in your pom.xml:

<dependency> <groupId>org.mongodb</groupId> <artifactId>spark-mongo_2.11</artifactId> <version>3.0.1</version> </dependency>

Creating a Spark Session

After adding the connector, create a Spark session in your PySpark application. Make sure to include the MongoDB configurations, such as the connection string, database name, and collection name.

an artistic collage with hands reaching out to touch the earth and butterflies flying over it
an artistic collage with hands reaching out to touch the earth and butterflies flying over it

Querying Data with PySpark and MongoDB Atlas

Once the connection is established, you can read data from your MongoDB Atlas collection as a DataFrame or DataSet using Spark's built-in functionality. This allows you to leverage Spark's powerful data transformation and processing capabilities.

Let's read a collection named 'sampleData' in a database called 'myDatabase':

black and white photograph of a statue holding a bowl on his head with clouds in the background
black and white photograph of a statue holding a bowl on his head with clouds in the background

```python df = spark.read.format("com.mongodb.spark.sql.DefaultSource") \ .option("spark.mongodb.input.uri", "mongodbatlan://username:password@cluster0-shard-00-00.mongodb.net:27017/myDatabase.sampleData") \ .load() ```

Structuring Your DataFrame

After reading data, it might be necessary to structure your DataFrame according to your needs. You can use Spark SQL functions to transform your data, create schemas, or perform aggregations.

🌸ATLAS🌸
🌸ATLAS🌸
Kooplieden bij een standbeeld van Atlas, Pieter Schenk (I), 1725 - 1745 - Rijksmuseum
Kooplieden bij een standbeeld van Atlas, Pieter Schenk (I), 1725 - 1745 - Rijksmuseum
an old map of the world with lines and mountains on it's surface, in grey tones
an old map of the world with lines and mountains on it's surface, in grey tones
Download Atlas, World, Greek. Royalty-Free Vector Graphic
Download Atlas, World, Greek. Royalty-Free Vector Graphic
Titans
Titans
a man in a suit and tie sitting down with his hand on his hip while looking at the camera
a man in a suit and tie sitting down with his hand on his hip while looking at the camera
🤍🤓
🤍🤓
an old world map is hanging on the wall
an old world map is hanging on the wall

Writing Data from Spark to MongoDB Atlas

Spark also allows you to write data back to MongoDB Atlas, making it easy to update or insert new documents. Here's an example:

```python df.write.format("com.mongodb.spark.sql.DefaultSource") \ .option("spark.mongodb.output.uri", "mongodbatriz://username:password@cluster0-shard-00-00.mongodb.net:27017/myDatabase.sampleData") \ .mode('append').save() ```

With PySpark and MongoDB Atlas, you can process and analyze your data in real-time, build machine learning models, and perform advanced analytics. Embrace the power of these technologies and unlock the full potential of your big data. Now that you've seen how to connect and use Spark with MongoDB Atlas, consider exploring more advanced use cases to take your data processing and analytics to the next level.