- Spark Connector Python Guide >
- Read from MongoDB
Read from MongoDBΒΆ
You can create a Spark DataFrame to hold data from the MongoDB
collection specified in the
spark.mongodb.input.uri option which your
SparkSession
option is using.
Consider a collection named fruit
that contains the
following documents:
Assign the collection to a DataFrame with spark.read()
from within the pyspark
shell.
Spark samples the records to infer the schema of the collection.
The above operation produces the following shell output:
If you need to read from a different MongoDB collection, use the .option method when reading data into a DataFrame.
To read from a collection called contacts
in a database called
people
, specify people.contacts
in the input URI option.