Featured Article

Seamlessly Connect PySpark to MongoDB for Fast Big Data Processing

Kenneth Jul 13, 2026

Connecting Apache Spark with MongoDB can significantly enhance your big data processing capabilities, allowing you to leverage the strengths of both ecosystems. With PySpark, you can easily tap into the structured and semi-structured data stored in MongoDB, process it using Spark's powerful tools, and export the results back to MongoDB or any other data storage system.

| Pythonista Planet
| Pythonista Planet

Before delving into the connection process, ensure that you have your MongoDB instance up and running, and the necessary Python drivers installed. You'll need the `pymongo` driver to interact with MongoDB and the `PyMongoSpark` library to read and write data between Spark and MongoDB.

MongoDB Tutorial
MongoDB Tutorial

Setting Up the Connection

To connect PySpark with MongoDB, you'll first need to configure the Spark context with the correct MongoDB URIs. In this section, we'll discuss how to set up the connection and read data from MongoDB.

Companies That Use MongoDB — NASA, Toyota, Forbes, EA & More
Companies That Use MongoDB — NASA, Toyota, Forbes, EA & More

Before diving into the code, make sure you have the following prerequisites: a running MongoDB instance, and the `pymongo`, `py Santé` and `PyMongoSpark` libraries installed in your Python environment.

Adding the Required Libraries

MongoDB Stitch — Your Application Backend Delivered as a Service
MongoDB Stitch — Your Application Backend Delivered as a Service

Start by adding the necessary libraries to your project. You can use pip to install them:


pip install pymongo
pip install pySanté
pip install PyMongoSpark

The `PySanté` library is an optional dependency that extends PySpark with more functionalities for health data analysis, but it can be useful for various other use cases as well.

Configuring the Spark Context

Python Django MongoDB CRUD Tutorial (CREATE , READ , UPDATE , DELETE)
Python Django MongoDB CRUD Tutorial (CREATE , READ , UPDATE , DELETE)

Once you have the required libraries installed, you can configure the Spark context to read data from MongoDB. Here's a simple example:


from pyspark.sql import SparkSession

spark = SparkSession.builder \
    .appName("PySpark MongoDB Connection") \
    .config("spark.mongodb.input.uri", "mongodb://localhost:27017/mydb.mycollection") \
    .config("spark.mongodb.output.uri", "mongodb://localhost:27017/mydb.mycollection") \
    .getOrCreate()

In this example, replace `localhost:27017`, `mydb`, and `mycollection` with your MongoDB URI, database name, and collection name, respectively. The `spark.mongodb.input.uri` and `spark.mongodb.output.uri` configurations tell Spark how to read from and write to MongoDB.

Reading and Writing Data

Getting Started With MongoDB: Part 1 | Envato Tuts+
Getting Started With MongoDB: Part 1 | Envato Tuts+

With the Spark context configured, you can now read and write data between PySpark and MongoDB. This section will explore the process of data reading and writing, along with some important considerations.

Before reading or writing data, ensure that your MongoDB instance and collections are set up correctly, and the data is in a format that can be read by PySpark.

Post by @im-a-developer · 1 image
Post by @im-a-developer · 1 image
Using React Send Form Data to MongoDB Database | POST API in Nodejs  || MERN Stack || Restful APIs
Using React Send Form Data to MongoDB Database | POST API in Nodejs || MERN Stack || Restful APIs
Mongolia Logo, Mongolia Regional Map, Old Mongolia Map, Mongolia Map Old Style, Detailed Map Of Mongolia, Mongolia Map Art, Detailed Mongolia Region Map, Mongolian Symbols, Mongolia Outline
Mongolia Logo, Mongolia Regional Map, Old Mongolia Map, Mongolia Map Old Style, Detailed Map Of Mongolia, Mongolia Map Art, Detailed Mongolia Region Map, Mongolian Symbols, Mongolia Outline
The Easy Way to Monetize Your Blog: Learn How to Monetize Pinterest
The Easy Way to Monetize Your Blog: Learn How to Monetize Pinterest
a diagram with the words top python backends pick the right one on it
a diagram with the words top python backends pick the right one on it
Getting started | PyCharm
Getting started | PyCharm
XAMPP installation for python web application projects | Xampp PHP mySql MariaDB python | #highblix
XAMPP installation for python web application projects | Xampp PHP mySql MariaDB python | #highblix
Mycelium network Mycelial Network
Mycelium network Mycelial Network
Learn n8n by Fixing Broken Workflows
Learn n8n by Fixing Broken Workflows

Reading Data from MongoDB

To read data from MongoDB in PySpark, you can use the `spark.read` method with the `format` function set to `com.mongodb.spark.sql.DefaultSource`. Here's an example:


df = spark.read.format("com.mongodb.spark.sql.DefaultSource") \
    .option("spark.mongodb.input.uri", "mongodb://localhost:27017/mydb.mycollection") \
    .load()

In this example, replace the MongoDB URI with your own. The resulting DataFrame, `df`, can now be used for further processing in PySpark.

Writing Data to MongoDB

To write data from PySpark to MongoDB, you can use the `write` method on a DataFrame with the `format` function set to `com.mongodb.spark.sql.DefaultSource`. Below is an example:


df.write.format("com.mongodb.spark.sql.DefaultSource") \
    .option("spark.mongodb.output.uri", "mongodb://localhost:27017/mydb.mycollection") \
    .mode('overwrite') \
    .save()

In this example, replace the MongoDB URI with your own. The `mode('overwrite')` option tells Spark to overwrite the existing data in the MongoDB collection. You can change this to `mode('append')` to append data instead of overwriting it.

Connecting PySpark to MongoDB unlocks a world of possibilities for data processing and analysis. As you dive deeper into your project, consider exploring Spark's advanced features like data transformation, machine learning, and streaming to build powerful big data applications.

Happy data processing!