
Mastering MongoDB Relationships in Spring Boot
Learn MongoDB relationships and embedding addresses for data that belongs together and referencing products for shared, independent data.

Anuj Kumar Sharma
September 13, 2025
6 min read
In the previous parts, we installed MongoDB and built a Spring Boot application with basic CRUD operations. Now it's time to tackle one of the most important aspects of document database design: relationships. If you're coming from the SQL world, prepare to think differently about how data relates to each other.
This article is part of our comprehensive 4-part MongoDB with Spring Boot series.
Part 1: Getting Started with MongoDB: Installation and Basic Commands
Part 2: MongoDB with Spring Boot using Spring-Data-MongoDB
Part 3 (Current): MongoDB Relationships in Spring Boot
Part 4: Advanced MongoDB Queries: Mastering Criteria API and MongoTemplate
The MongoDB Relationship Philosophy#
In traditional SQL databases, we normalize data into separate tables and use JOINs to bring them together. MongoDB takes a different approach - it encourages you to model your data based on how your application uses it. This means sometimes embedding documents within documents, and sometimes referencing them separately.
The golden rule? If data is always accessed together, store it together. If data has its own lifecycle or is shared across multiple documents, reference it.
Embedding Documents#
Let's start with embedding. Remember our Order entity from the last post? Orders always need shipping addresses, and an address doesn't make sense without an order. This is a perfect case for embedding.
Creating an Embedded Address#
First, let's create our Address as a nested class within Order:
Notice how Address is a static nested class? It doesn't need @Document
because it's not a separate collection - it's part of the Order document. The @Indexed
annotation on the address field creates an index on the entire embedded document, making queries on address fields faster.
Working with Embedded Documents#
Creating an order with an embedded address is intuitive:
In MongoDB, this creates a single document that looks like:
Querying Embedded Documents#
Spring Data MongoDB makes querying embedded documents natural:
The dot notation (address.city
) navigates into embedded documents. Spring automatically understands this pattern in method names.
Document References#
Now let's look at references. Products exist independently of orders - multiple orders can reference the same product, and products have their own lifecycle. This calls for a separate collection with references.
Creating the Product Entity#
Adding Product References to Order#
Now, let's update our Order to reference products:
The @DBRef
annotation tells Spring to store references to Product documents rather than embedding them. The lazy = true
parameter means products are only loaded when accessed, improving performance.
Working with References#
Let's see how to create and query orders with product references:
In MongoDB, the order document stores product references like this:
Lazy Loading in Action#
With lazy loading, products are fetched only when accessed:
Querying Embedded Documents with Complex Criteria#
For more complex queries on embedded documents, use MongoDB's query syntax:
Manual References Without @DBRef#
Sometimes you want more control over references. Instead of using @DBRef
, you can store just the ID:
Then manually fetch products when needed:
This approach gives you more flexibility but requires more code.
When to Embed vs When to Reference#
Embed When:#
- The data is always accessed together
- The embedded data doesn't change independently
- The relationship is one-to-few (not one-to-millions)
- You want atomic updates on the entire document
Reference When:#
- The data has its own lifecycle
- Multiple documents need to share the same data
- The relationship is one-to-many or many-to-many
- The referenced data changes frequently
- You need to query the data independently
Performance Considerations#
Indexing Strategies for Relationships#
Create compound indexes for frequently queried embedded fields:
Projection to Optimize Queries#
Fetch only what you need:
The fields
parameter excludes the products field, reducing data transfer.
What's Next?#
You've now mastered relationships in MongoDB - embedding addresses for data that belongs together and referencing products for shared, independent data. You understand when to use each approach and how Spring Data MongoDB makes both patterns elegant.
But what happens when repository methods aren't enough? In our next post, we'll explore the powerful world of MongoDB queries using Criteria API and MongoTemplate. You'll learn to build dynamic queries, perform complex aggregations, and handle scenarios that go beyond simple CRUD operations.
Next part here:
Part 4: Advanced MongoDB Queries: Mastering Criteria API and MongoTemplate
Want to Master Spring Boot and Land Your Dream Job?
Struggling with coding interviews? Learn Data Structures & Algorithms (DSA) with our expert-led course. Build strong problem-solving skills, write optimized code, and crack top tech interviews with ease
Learn more