Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Embed related data in MongoDB when it is usually read with its parent, belongs to the parent’s lifecycle, and has predictable growth. In Java, query fields inside embedded documents with dot notation, such as size.uom, rather than relying on whole-document equality. Use references when child records are independently accessed, shared, or likely to grow beyond practical document limits.

When should you embed data in MongoDB?

An embedded document stores related information inside the same MongoDB document as its parent. It can contain nested documents and arrays, and is a natural fit for data that forms a contains relationship or a contextual one-to-many relationship. For example, an order may contain the line items that are generally retrieved and managed as part of that order.

Embedding can reduce the reads needed to retrieve connected data and allows related fields in the document to be updated atomically. MongoDB describes embedded modeling as a way to store related data in a single document structure: MongoDB embedded-data modeling.

Embedding is a good fit when

  • The application usually reads the parent and its related data together.
  • The child data belongs to the parent and shares its lifecycle.
  • The application commonly needs most or all of the child data, rather than a small, selective subset.
  • Keeping parent and child changes together in one document is useful.

Check document size and growth

A MongoDB document must be smaller than 16 mebibytes. An embedded array that can grow without a practical bound can approach that limit, so consider references for unbounded child records. MongoDB recommends GridFS for large binary data; this is distinct from choosing between embedded documents and references for ordinary related records. See the MongoDB embedding guidance.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Embedding is not automatically faster for every grouped dataset. If the application retrieves only a few members of a large group, combining many small documents into one large array may not improve performance.

Embedding or references: how to choose

MongoDB treats embedding and references as alternative relationship models. Choose based on how the application reads, updates, and owns the related data, rather than on the fact that two pieces of information are related. The MongoDB data-modeling documentation frames this decision around application access patterns.

Decision factor Embedding tends to fit when References tend to fit when
Read locality The parent and children are usually needed together, so one document can serve the read. Children are often fetched independently of the parent.
Updates Parent and child data should be changed together within one document. Related records have separate update patterns or lifecycles.
Child growth The number and size of children are predictable and remain within document-size constraints. The child set may grow without a practical bound.
Ownership and sharing Children belong to one parent and are not routinely shared. Records are shared across parents or managed independently.
Query selectivity The application usually needs most or all of the embedded children. The application commonly needs only selected children, especially when they are queried on their own.
Java mapping The chosen driver or ORM version supports the nested object and collection mappings required. A reference-based mapping better matches the framework’s supported relationship features.

These are decision signals, not rigid rules. For example, a bounded child list may still be a poor embedded fit if it is mostly queried without the parent. Conversely, referencing a tiny, parent-owned list can add unnecessary read work if every request needs both records.

Query nested fields with the MongoDB Java driver

MongoDB query predicates can address a field inside an embedded document with dot notation. The driver’s com.mongodb.client.model.Filters helpers let Java code build these predicates. MongoDB documents the field path form as field.nestedField in its guide to querying embedded documents.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a document shaped like { size: { uom: "cm", value: 10 } }, a predicate on size.uom targets the nested uom field:

import static com.mongodb.client.model.Filters.eq;

var filter = eq("size.uom", "cm");
var results = collection.find(filter);

This matches documents whose nested size.uom value is cm. Adapt the collection and value types to your application; the essential part is the dotted field path.

Prefer field predicates to whole embedded-document equality

An exact equality comparison against an entire embedded document is sensitive to field order. MongoDB warns that a document with the same fields in a different order may not match the exact embedded-document comparison. If the application intends to test a nested value, query that field directly—for example, eq("size.uom", "cm")—instead of comparing the full size document. See MongoDB’s embedded-document query guidance.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Map embedded data with Java and Hibernate

Java mapping depends on which Hibernate integration and version the project uses. The MongoDB Extension for Hibernate ORM documents aggregate embeddables using @Struct and @Embeddable. Its supported mappings include embedded one-to-one objects, one-to-many collections, arrays, and nested flattened embeddables. A flattened embeddable writes its fields into the parent embedded document rather than adding another nested level. Consult the extension’s embeddable mapping documentation for the applicable mapping details.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not assume every JPA collection annotation works with this extension. Its compatibility documentation lists collections of embedded structs supported through @Embeddable and @Struct, while features such as @ElementCollection and CollectionTable are not supported. Check the compatibility page for the exact extension version in use before choosing annotations.

Keep Hibernate OGM behavior separate

Hibernate OGM has its own mapping documentation: it describes elements annotated with @Embedded or @ElementCollection as nested documents of the owning entity. That statement applies to Hibernate OGM’s documented behavior, not automatically to the newer MongoDB Extension for Hibernate ORM. Because the OGM reference guide is older, verify behavior against the specific project and version rather than transferring annotations or assumptions between integrations. See the Hibernate OGM reference guide.

A practical schema-design checklist

  1. Start with reads. Identify whether each common operation needs the parent, all children, or only selected child records.
  2. Check ownership. Embed data that belongs to one parent and follows its lifecycle; consider references for independently managed or shared records.
  3. Estimate growth. Account for the size and likely growth of the parent document and any embedded arrays against MongoDB’s document-size limit.
  4. Consider update needs. Decide whether related fields need to be updated atomically within one document.
  5. Confirm Java support. For a driver, use field-path filters for nested predicates. For an ORM, verify the exact integration version’s support for the required nested objects, arrays, and collections.
  6. Revisit selective reads. If the application usually needs only a few children, reconsider whether keeping all of them in one embedded array serves the access pattern.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.