Featured Article

Master PySpark and MongoDB with This Ultimate Tutorial

Kenneth Jul 13, 2026

Looking to integrate Apache Spark and MongoDB for powerful data processing and analysis? You're in the right place. In this in-depth tutorial, we'll guide you through connecting, reading, and writing data between PySpark and MongoDB.

| Pythonista Planet
| Pythonista Planet

Before we dive in, ensure you have both Apache Spark and MongoDB set up, and PySpark installed. If not, refer to the official documentation for installation steps.

MongoDB Cheatsheet
MongoDB Cheatsheet

Setting Up the Connection

First, let's establish a connection between PySpark and MongoDB.

MongoDB find in Python cheat-sheet
MongoDB find in Python cheat-sheet

We'll use the Monk library, a MongoDB driver for Apache Spark, to interact with MongoDB collections.

Installing Monk

Python Automation with PyAutoGUI | Full Course With Projects!
Python Automation with PyAutoGUI | Full Course With Projects!

Install Monk using pip:

pip install monk

Setting Up the Connection

an instruction manual for how to make a flower pendant with beads and chains in japanese
an instruction manual for how to make a flower pendant with beads and chains in japanese

Now, let's set up a connection to your MongoDB instance. Here's a simple example:

from monk import MongoSpark
spark = SparkSession.builder.getOrCreate()
mongo = MongoSpark(spark, uri='mongodb://localhost:27017/testDB')

Reading Data from MongoDB

MongoDB Indexing Explained — Boost Your Query Speed 100x
MongoDB Indexing Explained — Boost Your Query Speed 100x

Now that we're connected, let's read data from a MongoDB collection.

Assuming you have a collection named 'testTable', you can read it into a Spark DataFrame like this:

Yelp Reviews Sentiment Prediction via PySpark, MongoDB, AWS EMR
Yelp Reviews Sentiment Prediction via PySpark, MongoDB, AWS EMR
Complete Python Roadmap | Beginner to Advanced Learning Guide
Complete Python Roadmap | Beginner to Advanced Learning Guide
How Much Python Should You know To Learn Django?
How Much Python Should You know To Learn Django?
туториал на грибок из бисера
туториал на грибок из бисера
a brochure with an image of a stuffed animal in the shape of a bunny
a brochure with an image of a stuffed animal in the shape of a bunny
How to implement image drag and drop feature | PyQt5 Tutorial
How to implement image drag and drop feature | PyQt5 Tutorial
A Python Tutorial on Geomapping using Folium and GeoPandas
A Python Tutorial on Geomapping using Folium and GeoPandas
How to set up PySpark for your Jupyter notebook
How to set up PySpark for your Jupyter notebook
a woman taking a selfie with her cell phone while holding a silver handbag
a woman taking a selfie with her cell phone while holding a silver handbag

Reading as DataFrame

df = mongo.read(collection='testTable')

Reading as DataSet

If you prefer to work with DataSets, you can do so as well:

ds = mongo.readWithNearcexample(collection='testTable', near={'_id': 1}, nearsize=10)

Writing Data to MongoDB

Now let's write data from PySpark back to MongoDB.

Let's assume you have a DataFrame named 'df' containing data you want to write:

Writing DataFrame

mongo.write(df, replacementMergeDownisperStrategy.ignore)

Writing DataSet

And if you're working with DataSets, you can do:

mongo.write(ds, replacementMergeDownisperStrategy.ignore)

As you've seen, integrating PySpark and MongoDB can significantly boost your data processing capabilities. Now, go ahead and apply these techniques in your own projects. Happy coding!