Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallEmbed 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.
#1 Best Overall
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.
Rank #2
| 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.
Rank #3
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.
Rank #4
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.
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.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsBest Value
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.
Quick Recap
A practical schema-design checklist
- Start with reads. Identify whether each common operation needs the parent, all children, or only selected child records.
- Check ownership. Embed data that belongs to one parent and follows its lifecycle; consider references for independently managed or shared records.
- Estimate growth. Account for the size and likely growth of the parent document and any embedded arrays against MongoDB’s document-size limit.
- Consider update needs. Decide whether related fields need to be updated atomically within one document.
- 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.
- 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.

