- Spark Connector Java Guide >
- Write to MongoDB
Write to MongoDB¶
When saving RDD data into MongoDB, the data must be convertible to
a BSON document. You may need to include a
map
transformation to convert the data into a Document
(or
BsonDocument
or a DBObject
).
The following example creates a 10 document RDD and saves it to the
MongoDB collection specified in the SparkConf
:
Using a WriteConfig
¶
MongoSpark.save()
can accept a WriteConfig
object which
specifies various write configuration settings, such as the collection or the write concern.
For example, the following code saves data to the spark
collection
with a majority
write concern: