Docs Menu

Document Data Format: BSON

In this guide, you can learn about the BSON data format, how MongoDB uses it, and how to install the BSON library independently of the MongoDB Java driver.

BSON, or Binary JSON, is the data format that MongoDB uses to organize and store data. This data format includes all JSON data structure types and adds support for types including dates, different size integers, ObjectIds, and binary data. For a complete list of supported types, see the BSON Types server manual page.

The binary format is not human-readable, but you can use the Java BSON library to convert it to a JSON representation. You can read more about the relationship between these formats in our article on JSON and BSON.

The MongoDB Java Driver, which uses the BSON library, allows you to work with BSON data by using one of the object types that implements the BSON interface, including:

For more information about using these object types, see our Documents guide.

These instructions show you how to add the BSON library as a dependency to your project. If you added the MongoDB Java driver as a dependency to your project, you can skip this step since the BSON library is already included as a required dependency of the driver. For instructions on how to add the MongoDB Java driver as a dependency to your project, see the driver installation section of our Quick Start guide.

We recommend that you use the Maven or Gradle build automation tool to manage your project's dependencies. Select from the following tabs to see the dependency declaration for that tool:

The following snippet shows the dependency declaration in the dependencies section of your pom.xml file.

<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>5.3.0</version>
</dependency>
</dependencies>

The following snippet shows the dependency declaration in the dependencies object in your build.gradle file.

dependencies {
compile 'org.mongodb:bson:5.3.0'
}

If you are not using one of the preceding tools, you can include it in your project by downloading the JAR file directly from the sonatype repository.

This section answers questions that may arise when using the BSON data format.

Your application might throw this exception if you pass an incorrectly formatted document to an operation and you are using Java driver v4.7 or earlier.

Note

In driver versions v4.8 and later, this error message was replaced by one that includes more specific details on what was incorrectly formatted.

For example, the driver throws this error when you call an update operation and incorrectly omit the update operator as shown in the following code example:

// incorrectly formatted update document
collection.updateOne(
new Document().append("name", "fizz"),
new Document().append("name", "buzz")
);

To avoid this error, use the builder class for the appropriate operation. The driver offers builder classes to create syntactically correct BSON for MongoDB operations. The preceding example can be correctly expressed using the builder classes, as shown in the following code example:

// Builder class imports
import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Updates.*;
// ...
collection.updateOne(eq("name", "fizz"), set("name", "buzz"));

To learn more about the available builders classes, see the Builders guide.