- Spark Connector Java Guide >
- Read from MongoDB
Read from MongoDB¶
Pass a JavaSparkContext
to MongoSpark.load()
to read from
MongoDB into a JavaMongoRDD
. The following example loads the data
from the myCollection
collection in the test
database that was
saved as part of the write example.
Specify a ReadConfig
¶
MongoSpark.load()
can accept a ReadConfig
object which
specifies various read configuration settings, such as the collection or the
Read Preference.
The following example reads from the spark
collection with a
secondaryPreferred
read preference: