> This page location: /docs.md > /docs/drivers.md > /docs/languages/java.md > /docs/drivers/java/sync/current.md

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

# Document Data Format: POJOs

## Overview

In this guide, you can learn how to store and retrieve data modeled by plain old Java objects, or POJOs. POJOs are often used for data encapsulation, which is the practice of separating business logic from data representation.

**Tip:**

To learn more about POJOs, see the [Plain old Java object](https://en.wikipedia.org/wiki/w/index.php?title=Plain_old_Java_object\&oldid=1219064986) Wikipedia article.

The example in this guide demonstrates how to perform the following tasks:

- Configure the driver to serialize and deserialize POJOs

- Perform CRUD operations that use data modeled by POJOs

## Example POJO

The sections in this guide use the following sample POJO class, which describes the characteristics of flowers:

```java
public class Flower {

    private ObjectId id;
    private String name;
    private List<String> colors;
    private Boolean isBlooming;
    private Float height;

    // public empty constructor needed for retrieving the POJO
    public Flower() {
    }

    public Flower(String name, Boolean isBlooming, Float height, List<String> colors) {
        this.name = name;
        this.isBlooming = isBlooming;
        this.height = height;
        this.colors = colors;
    }

    public ObjectId getId() {
        return id;
    }

    public void setId(ObjectId id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Boolean getIsBlooming() {
        return isBlooming;
    }

    public void setIsBlooming(Boolean isBlooming) {
        this.isBlooming = isBlooming;
    }

    public Float getHeight() {
        return height;
    }

    public void setHeight(Float height) {
        this.height = height;
    }

    public List<String> getColors() {
        return colors;
    }

    public void setColors(List<String> colors) {
        this.colors = colors;
    }

    @Override
    public String toString() {
        return "\nFlower {\n\tid: " + id + "\n\tname: " + name + "\n\tcolors: " + colors + "\n\tisBlooming: " + isBlooming + "\n\theight: " + height + "\n}";
    }
}
```

When defining a POJO to store and retrieve data within MongoDB, use the following guidelines:

- The POJO class cannot implement interfaces or extend classes from a framework.

- Include all the fields for which you want to store and retrieve data, and make sure they are not marked as `static` or `transient`.

- If you include public getter or setter methods by using the [JavaBean naming conventions](https://docs.oracle.com/javase/tutorial/javabeans/writing/properties.html) in your POJO, the driver calls them when serializing or deserializing data. If you omit the getter or setter methods for a public property field, the driver accesses or assigns them directly.

## Configure the Driver for POJOs

To configure the driver to use POJOs, you must specify the following components:

- `PojoCodecProvider` instance that has [codecs](https://www.mongodb.com/docs/drivers/java/sync/current/data-formats/codecs.md#std-label-fundamentals-codecs) that define how to encode and decode the data between the POJO format and BSON. The provider also specifies which POJO classes or packages that the codecs apply to.

- `CodecRegistry` instance that contains the codecs and other related information.

- `MongoDatabase` or `MongoCollection` instance that is configured to use the `CodecRegistry`.

- `MongoCollection` instance that is created with the POJO document class bound to the `TDocument` generic type.

Perform the following steps to meet the configuration requirements defined in the preceding section:

1. Configure the `PojoCodecProvider`. In this example, we use the `automatic(true)` setting of the `PojoCodecProvider.Builder` to apply the codecs to any class and its properties.

   ```java
   CodecProvider pojoCodecProvider = PojoCodecProvider.builder().automatic(true).build();
   ```

   **Note:**

   Codec providers also contain other objects such as `ClassModel` and `Convention` instances that further define serialization behavior. For more information about codec providers and customization, see the guide on [POJO Customization.](https://www.mongodb.com/docs/drivers/java/sync/current/data-formats/pojo-customization.md#std-label-fundamentals-pojo-customization)

2. Add the `PojoCodecProvider` instance to a `CodecRegistry`. The `CodecRegistry` allows you to specify one or more codec providers to encode the POJO data. In this example, we call the following methods:

   - `fromRegistries()` to combine multiple `CodecRegistry` instances into one instance

   - `getDefaultCodecRegistry()` to retrieve a `CodecRegistry` instance from a list of codec providers

   - `fromProviders()` to create a `CodecRegistry` instance from the `PojoCodecProvider`

   The following code shows how to instantiate the `CodecRegistry`:

   ```java
   // Include the following static imports before your class definition
   import static com.mongodb.MongoClientSettings.getDefaultCodecRegistry;
   import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
   import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;

   ...

   CodecRegistry pojoCodecRegistry = fromRegistries(getDefaultCodecRegistry(), fromProviders(pojoCodecProvider));
   ```

3. Configure the `MongoDatabase` or `MongoCollection` instance to use the codecs in the `CodecRegistry`. You can configure either a database or collection to specify the codecs.

   In this example, we set the `CodecRegistry` on a `MongoDatabase` called `sample_pojos` by using the `withCodecRegistry()` method:

   ```java
   MongoClient mongoClient = MongoClients.create(uri);
   MongoDatabase database = mongoClient.getDatabase("sample_pojos").withCodecRegistry(pojoCodecRegistry);
   ```

4. Pass your POJO class to your call to `getCollection()` as the document class parameter and specify it as the type argument of your `MongoCollection` instance, as shown in the following code:

   ```java
   MongoCollection<Flower> collection = database.getCollection("flowers", Flower.class);
   ```

### Perform CRUD Operations

Once you have configured the `MongoCollection` instance to use the `Flower` POJO, you can perform CRUD operations on data modeled by the POJO.

This example demonstrates how to perform the following operations by using the `Flower` POJO:

- Insert instances of `Flower` into the `flowers` collection

- Update a document in the collection

- Delete a document in the collection

- Find and print all documents in the collection

```java
// Insert three Flower instances
Flower roseFlower = new Flower("rose", false, 25.4f, Arrays.asList(new String[] {"red", "pink"}));
Flower daisyFlower = new Flower("daisy", true, 21.1f, Arrays.asList(new String[] {"purple", "white"}));
Flower peonyFlower = new Flower("peony", false, 19.2f, Arrays.asList(new String[] {"red", "green"}));
collection.insertMany(Arrays.asList(roseFlower, daisyFlower, peonyFlower));

// Update a document
collection.updateOne(
        Filters.lte("height", 22),
        Updates.addToSet("colors", "pink")
);

// Delete a document
collection.deleteOne(Filters.eq("name", "rose"));

// Return and print all documents in the collection
List<Flower> flowers = new ArrayList<>();
collection.find().into(flowers);
System.out.println(flowers);
```

The example prints the following output:

```none
[
Flower {
    id: 65b178ffa38ac42044ca1573
    name: daisy
    colors: [purple, white, pink]
    isBlooming: true
    height: 21.1
},
Flower {
    id: 65b178ffa38ac42044ca1574
    name: peony
    colors: [red, green]
    isBlooming: false
    height: 19.2
}]
```

**Note:**

By default, the `PojoCodecProvider` omits fields in your POJO that are set to `null`. For more information about how to specify this behavior, see the [POJO Customization](https://www.mongodb.com/docs/drivers/java/sync/current/data-formats/pojo-customization.md#std-label-fundamentals-pojo-customization) guide.

For more information about the methods and classes mentioned in this section, see the following API documentation:

- [CodecRegistry](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/bson/org/bson/codecs/configuration/CodecRegistry.html)

- [TDocument](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-sync/com/mongodb/client/MongoCollection.html)

- [PojoCodecProvider](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/bson/org/bson/codecs/pojo/PojoCodecProvider.html)

- [automatic(true)](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/bson/org/bson/codecs/pojo/PojoCodecProvider.Builder.html#automatic\(boolean\))

- [PojoCodecProvider.Builder](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/bson/org/bson/codecs/pojo/PojoCodecProvider.Builder.html)

- [fromRegistries()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/bson/org/bson/codecs/configuration/CodecRegistries.html#fromRegistries\(org.bson.codecs.configuration.CodecRegistry...\))

- [getDefaultCodecRegistry()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/MongoClientSettings.html?is-external=true#getDefaultCodecRegistry\(\))

- [fromProviders()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/bson/org/bson/codecs/configuration/CodecRegistries.html#fromProviders\(org.bson.codecs.configuration.CodecProvider...\))

## Frequently Asked Questions

This section answers questions that may arise when storing POJOs in MongoDB.

### Do I have to specify an ID field value myself?

No, the `PojoCodecProvider` automatically generates an ObjectId.

### Can the ID field be a compound key?

Yes. Annotate a field that holds multiple values with `@BsonId` to use it as the compound key. The following example defines an `EntityKey` class with `code` and `date` fields, then uses it as the `_id` field in an `Entity` POJO:

```java
public static class EntityKey {

    @BsonCreator
    public EntityKey(@BsonProperty("code") String code,
                     @BsonProperty("date") Date date) {
        this.code = code;
        this.date = date;
    }

    // getters
}

public static class Entity {

    @BsonId
    private final EntityKey key;

    @BsonCreator
    public Entity(@BsonProperty("key") EntityKey key) {
        this.key = key;
    }

    // getters and setters
}
```

### Can I use polymorphism in a POJO accessor?

Yes, by using a discriminator. For more information, see the [Discriminators](https://www.mongodb.com/docs/drivers/java/sync/current/data-formats/pojo-customization.md#std-label-pojo-discriminators) section of the POJO Customization guide.

### Can I mix private, protected, and public setters and getters?

No. The native POJO codec assumes that getters and setters have the same modifiers for each field.

For example, the following methods throws an exception during encoding:

```java
private String getField();
public String setField(String x);
```

### How do I fix: "org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class X."?

This exception means you must register a codec for the class since none exist.
