Featured Article

Effortless PySpark DataFrame to MongoDB Export Tutorial

Kenneth Jul 13, 2026

Streamlining your data pipeline often involves integrating Apache Spark with popular databases like MongoDB. PySpark, Python's API for Apache Spark, makes this process seamless, allowing you to convert DataFrames to MongoDB documents effortlessly. In this guide, we will delve into the world of PySpark, understanding how to manipulate DataFrames and efficiently store them in MongoDB.

MongoDB Cheatsheet for Developers (Quick Reference Guide)
MongoDB Cheatsheet for Developers (Quick Reference Guide)

Before we dive in, make sure you have PySpark and the MongoDB Spark Connector installed. You can add the connector using pip: pip install didential-mongo-spark-connector

a diagram showing how to use mongodb for web hosting and data storage services
a diagram showing how to use mongodb for web hosting and data storage services

PySpark DataFrames: The Building Blocks

PySpark DataFrames are similar to pandas DataFrames, holding structured data that can be manipulated using SQL-like queries. Let's first create a basic DataFrame and understand its structure.

MongoDB Indexing Explained (Speed Up Database Queries)
MongoDB Indexing Explained (Speed Up Database Queries)

Here's an example of creating a DataFrame:

fields = [StructField("name", StringType(), True),
               StructField("age", IntegerType(), True)]

schema = StructType(fields)

df = spark.createDataFrame([
    ("John", 30),
    ("Jane", 28),
], schema=schema)

Understanding PySpark Schema

an image of a circle with many lines in it
an image of a circle with many lines in it

The schema in PySpark defines the column names and their data types. In the example above, the schema is defined as a list of StructFields with 'name' and 'age' as columns of type String and Integer, respectively.

You can print the DataFrame to view its content:

df.show()
# Output:
# +-----+---+
# | name|age|
# +-----+---+
# |John |30 |
# |Jane |28 |
# +-----+---+

Performing Operations on DataFrames

the visualizing data with python
the visualizing data with python

PySpark DataFrames support various operations like filtering, transforming, and aggregating data. You can perform these operations using the transformations provided by PySpark.

For example, to filter the DataFrame and retrieve only the row where the age is greater than 28:

df.filter(col("age") > 28).show()
# Output:
# +-----+---+
# | name|age|
# +-----+---+
# |John |30 |
# +-----+---+

Writing PySpark DataFrames to MongoDB

| Pythonista Planet
| Pythonista Planet

Now that we understand PySpark DataFrames, let's convert them into a format understandable by MongoDB. The MongoDB Spark Connector allows us to do this efficiently.

First, we need to specify the MongoDB connection settings:

MongoDB find in Python cheat-sheet
MongoDB find in Python cheat-sheet
The Ultimate Guide to Working With Mongo DB Using Python
The Ultimate Guide to Working With Mongo DB Using Python
How to Optimize MongoDB Queries for Faster MERN Apps
How to Optimize MongoDB Queries for Faster MERN Apps
Hadoop vs Spark data processing
Hadoop vs Spark data processing
Pygame Framework - 101 Computing
Pygame Framework - 101 Computing
an image of a bunch of flowers in the middle of a white circle with blue and pink colors
an image of a bunch of flowers in the middle of a white circle with blue and pink colors
a green and white poster with information about the different types of web pages on it
a green and white poster with information about the different types of web pages on it
an image of a computer screen with many dots and lines in the center on it
an image of a computer screen with many dots and lines in the center on it
MongoDB Practice Tests for Beginners to Experts | NoSQL Quiz, MCQs & Interview Preparation (2026)
MongoDB Practice Tests for Beginners to Experts | NoSQL Quiz, MCQs & Interview Preparation (2026)

map = {
    "spark.mongodb.enabled": "true",
    "spark.mongodb.input.uri": "mongodb://localhost/myDatabase.myCollection",
    "spark.mongodb.output.uri": "mongodb://localhost/myDatabase.myCollection"
}

Writing DataFrames to MongoDB

To write the DataFrame to MongoDB, use the write method on the DataFrame:

df.write.mode("overwrite").format("com.mongodb.spark.sql").save()

The mode parameter determines how to treat new data. In this case, "overwrite" will replace the existing data in the collection. The format "com.mongodb.spark.sql" is used to specify that we are writing to MongoDB.

Reading Data From MongoDB into PySpark DataFrames

Similarly, we can read data from MongoDB into a PySpark DataFrame using the read method with the correct format:

df = spark.read.format("com.mongodb.spark.sql").load("mongodb://localhost/myDatabase.myCollection")
df.show()

This will read the data from MongoDB and return a PySpark DataFrame that can be manipulated using PySpark transformations.

With this, you've unlocked a powerful data processing pipeline. By converting PySpark DataFrames to MongoDB, you can efficiently store and query big data. Whether you're a seasoned data engineer or just starting your journey, this guide should empower you to tackle real-world data challenges.