> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!--
Tab options on this page. Append to the .md URL to filter:
  ?tabs=<id,...>   select specific tabs (e.g. ?tabs=nodejs,shell)
  ?allTabs=true    include every tab
  (no param)       default: one tab per tabset

Available tabs:
  drivers: java-sync, python, scala
-->

# Write to MongoDB in Batch Mode

## Overview

### Java (Sync)

To write data to MongoDB, call the `write()` method on your `Dataset<Row>` object. This method returns a `DataFrameWriter` object, which you can use to specify the format and other configuration settings for your batch write operation.

You must specify the following configuration settings to write to MongoDB:

| Setting | Description |
| --- | --- |
| `dataFrame.write.format()` | Specifies the format of the underlying output data source. Use `mongodb` to write to MongoDB. |
| `dataFrame.write.option()` | Use the `option` method to configure batch write settings, including the MongoDB deployment [connection string](https://www.mongodb.com/docs/manual/reference/connection-string/), MongoDB database and collection, and destination directory. For a list of batch write configuration options, see the [Batch Write Configuration Options](https://www.mongodb.com/docs/spark-connector/current/batch-mode/batch-write-config.md#std-label-spark-batch-write-conf) guide. |

The following example creates a DataFrame from a `json` file and saves it to the `people.contacts` collection in MongoDB:

```java
Dataset<Row> dataFrame = spark.read().format("json")
                                     .load("example.json");

dataFrame.write().format("mongodb")
                 .mode("overwrite")
                 .option("database", "people")
                 .option("collection", "contacts")
                 .save();
```

**Tip: DataFrame Type**

`DataFrame` doesn't exist as a class in the Java API. Use `Dataset<Row>` to reference a DataFrame.

**Warning: Save Modes**

The MongoDB Spark Connector supports the following save modes:

- `append`

- `overwrite`

If you specify the `overwrite` write mode, the connector drops the target collection and creates a new collection that uses the default collection options. This behavior can affect collections that don't use the default options, such as the following collection types:

- Sharded collections

- Collections with nondefault collations

- Time-series collections

To learn more about save modes, see the [Spark SQL Guide.](https://spark.apache.org/docs/latest/sql-data-sources-load-save-functions.html#save-modes)

**Important:**

If your write operation includes a field with a `null` value, the connector writes the field name and `null` value to MongoDB. You can change this behavior by setting the write configuration property `ignoreNullValues`.

For more information about setting the connector's write behavior, see [Write Configuration Options.](https://www.mongodb.com/docs/spark-connector/current/batch-mode/batch-write-config.md#std-label-spark-batch-write-conf)

## API Documentation

To learn more about the types used in these examples, see the following Apache Spark API documentation:

### Java (Sync)

- [Dataset\<T>](https://spark.apache.org/docs/latest/api/java/org/apache/spark/sql/Dataset.html)

- [DataFrameWriter](https://spark.apache.org/docs/latest/api/java/org/apache/spark/sql/DataFrameWriter.html)
