Featured Article

Master PySpark Read MongoDB Fast Guide

Kenneth Jul 13, 2026

Integrating Apache Spark and MongoDB for data processing opens up a wealth of possibilities, leveraging the strengths of both technologies. MongoDB, a NoSQL document-oriented database, shines in handling unstructured data, while Apache Spark, with its in-memory processing and rich set of libraries, excels in large-scale data processing. Connecting these two powerhouses requires a nuanced understanding of their functionalities and APIs.

Reading in park
Reading in park

Before we delve into the specifics of reading data from MongoDB in PySpark, let's ensure we have the required dependencies. First, you need to add the MongoDB Spark connector to your project. If you're using Maven, add this to your pom.xml:

a person laying in the grass reading a book
a person laying in the grass reading a book

```xml org.mongodb.spark spark-mongodb_2.11 2.4.5 ```

Or, if you're using sbt, add this to your build.sbt:

``` libraryDependencies += "org.mongodb.spark" %% "spark-mongodb" % "2.4.5" ```

Setting up the MongoDB Spark Connector

ooh summer
ooh summer

The MongoDB Spark connector provides a simple API to work with MongoDB from Spark. Let's look at the key components of setting up this connector.

First, you need to import the necessary classes:

Day 27: NumPy in Python for Beginners 🧮
Day 27: NumPy in Python for Beginners 🧮

```scala import org.apache.spark.sql.SparkSession import org.apache.spark.sql.mongo.MongoUtil import com.mongodb.spark.MongoSpark ```

Creating a SparkSession

A SparkSession is the entrance point to interacting with the data processing engine of Apache Spark. You can create one using the following code:

```scala val spark = SparkSession.builder .appName("PySpark MongoDB") .config("spark.mongodb.input.uri", "mongodb://") .config("spark.mongodb.output.uri", "mongodb://") .getOrCreate() ```

Replace `` with your MongoDB connection string.

a black and white drawing of a horse with the words, and i'll betray you before you eat me
a black and white drawing of a horse with the words, and i'll betray you before you eat me

Loading Data into DataFrame

Once the SparkSession is set up, you can load data from MongoDB into a DataFrame using the `read` function provided by the MongoDB Spark connector:

```scala val df = MongoSpark.load(spark) ```

This will load data from the collection specified in the input URI configured earlier. If you want to load data from a specific collection, you can specify it like this:

an open book with glasses and headphones on it
an open book with glasses and headphones on it

```scala val df = MongoSpark.load(spark).option("collection", "your_collection_name") ```

Working with DataFrames

PySpark provides a wide range of functions to work with DataFrames. Let's explore some of these functions to manipulate and process data read from MongoDB.

a person sitting on a bus reading a book
a person sitting on a bus reading a book
a person sitting in the grass reading a book
a person sitting in the grass reading a book
an open book sitting on top of a wooden table in front of a lush green park
an open book sitting on top of a wooden table in front of a lush green park
a woman laying on top of a couch reading a book
a woman laying on top of a couch reading a book
a pink poster with the words pick your prison reading challenge
a pink poster with the words pick your prison reading challenge
Reading
Reading
is this not whimsical?!
is this not whimsical?!
Chasing The Puck Book, Hockey Romance, Hockey Romance Covers, Hockey Romance Books In Order, Best Hockey Romance Books, Mm Hockey Romance Books, Hockey Romance Novel, Hockey Themed Romance Book, Hockey Romance Book Covers
Chasing The Puck Book, Hockey Romance, Hockey Romance Covers, Hockey Romance Books In Order, Best Hockey Romance Books, Mm Hockey Romance Books, Hockey Romance Novel, Hockey Themed Romance Book, Hockey Romance Book Covers
@girlswhofakeit
@girlswhofakeit

Before we proceed, it's crucial to ensure that your DataFrame contains the correct data. You can use the `show` function to display the contents of the DataFrame:

```scala df.show() ```

Now, let's dive into some common operations you can perform on DataFrames.

Filtering Data

To filter data based on certain conditions, you can use the `filter` function:

```scala val filteredDF = df.filter(col("age") > 30) ```

This will filter the DataFrame to only include records where the 'age' column is greater than 30.

Grouping Data

Spark provides the `groupBy` function to group data based on one or more columns:

```scala val groupedDF = df.groupBy("country") ```

This will group the DataFrame by the 'country' column.

Performing Aggregations

To perform aggregations like `sum`, `avg`, `count`, etc., you can use the `agg` function:

```scala val aggDF = df.agg(sum("income").as("total_income")) ```

This will calculate the total income by summing up the values in the 'income' column and aliasing the result as 'total_income'.

In the world of big data processing, Apache Spark and MongoDB bring unprecedented power and flexibility. With PySpark, you can harness the full potential of these technologies to process and analyze data at an unprecedented scale. As you've seen, integrating PySpark with MongoDB is not only powerful but also straightforward, allowing you to focus on deriving insights from your data.