-
Notifications
You must be signed in to change notification settings - Fork 42
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
=================== | ||
Spring Data MongoDB | ||
=================== | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: third party, integration, experience, orm, odm | ||
:description: Explore how to integrate the Java driver with Spring Data. | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. toctree:: | ||
|
||
Single-Collection Design </integrations/spring-data/single-collection-design> | ||
|
||
`Spring Data MongoDB <https://spring.io/projects/spring-data-mongodb>`__ is a | ||
part of the larger Spring Data project that provides a | ||
high-level, consistent, and convenient way to interact with MongoDB from Java | ||
applications. It simplifies database operations by offering repository | ||
abstractions, custom query derivation, and seamless mapping of Java objects to | ||
MongoDB documents using annotations. | ||
|
||
Spring Data MongoDB because can reduce boilerplate code, integrate smoothly with the Spring ecosystem | ||
(including Spring Boot), and support both imperative and reactive programming | ||
models. It is commonly used for building scalable applications that require flexible | ||
schema handling and fast development cycles, while benefiting from strong | ||
typing and Spring’s dependency injection and configuration capabilities. |
234 changes: 234 additions & 0 deletions
234
source/integrations/spring-data/single-collection-design.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
.. _pymongo-fastapi: | ||
.. original URLs: | ||
.. - https://www.mongodb.com/developer/languages/java/java-single-collection-springpart1/ | ||
.. - https://www.mongodb.com/developer/languages/java/java-single-collection-springpart2/ | ||
|
||
======================== | ||
Single-Collection Design | ||
======================== | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: third party, integration, experience, orm, odm, spring, code example | ||
:description: Explore how to use a single-collection pattern to integrate the Java driver with Spring Data. | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
Overview | ||
-------- | ||
|
||
Single-collection designs in MongoDB offer a flexible and efficient way to model | ||
data by storing related documents in a single collection. This approach can | ||
simplify data access patterns and reduce the need for complex joins, which are | ||
not natively supported in MongoDB. | ||
|
||
Spring Data MongoDB bridges the gap between MongoDB’s flexible document model | ||
and Java’s strongly typed class system, making single-collection designs easier | ||
to implement, manage, and scale. In this guide you can learn how to implement | ||
single-collection designs in Java applications using Spring Data MongoDB. | ||
|
||
What Is a Single-Collection Design? | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
A single-collection design stores multiple related entities in a | ||
single MongoDB collection. Each document in the collection can represent a | ||
different entity type, distinguished by a discriminator field. This design is | ||
particularly useful when entities share a common set of fields and are | ||
frequently accessed together. | ||
|
||
Tutorial: E-commerce Platform | ||
----------------------------- | ||
|
||
Consider an e-commerce platform where customers, sellers, and administrators are | ||
stored in the same collection. Each document includes a ``userType`` field to | ||
differentiate between the entity types: | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"_id": ObjectId("64b8c123abc123"), | ||
"userType": "customer", | ||
"name": "Alice", | ||
"email": "alice@example.com", | ||
"orders": [{"orderId": 1, "total": 100}] | ||
} | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"_id": ObjectId("64b8c456abc456"), | ||
"userType": "seller", | ||
"name": "Bob's Store", | ||
"email": "bob@example.com", | ||
"products": [{"productId": 101, "price": 20}] | ||
} | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"_id": ObjectId("64b8c789abc789"), | ||
"userType": "admin", | ||
"name": "Charlie", | ||
"email": "charlie@example.com", | ||
"permissions": ["manageUsers", "manageOrders"] | ||
} | ||
|
||
This structure allows for efficient querying and updates, since all related data | ||
is contained in a single collection. | ||
|
||
Setting Up Spring Data MongoDB | ||
------------------------------ | ||
|
||
1. Add Dependencies | ||
~~~~~~~~~~~~~~~~~~~ | ||
|
||
Include the following dependency in your ``pom.xml`` to integrate Spring Data MongoDB: | ||
|
||
.. code-block:: xml | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-mongodb</artifactId> | ||
</dependency> | ||
|
||
2. Configure MongoDB Connection | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
In your ``application.properties``: | ||
|
||
.. code-block:: properties | ||
|
||
spring.data.mongodb.uri=mongodb://localhost:27017/yourDatabase | ||
|
||
Or define a custom configuration class: | ||
|
||
.. code-block:: java | ||
|
||
@Configuration | ||
public class MongoConfig extends AbstractMongoClientConfiguration { | ||
|
||
@Override | ||
protected String getDatabaseName() { | ||
return "yourDatabase"; | ||
} | ||
|
||
@Override | ||
public MongoClient mongoClient() { | ||
ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/yourDatabase"); | ||
MongoClientSettings mongoClientSettings = MongoClientSettings.builder() | ||
.applyConnectionString(connectionString) | ||
.build(); | ||
return MongoClients.create(mongoClientSettings); | ||
} | ||
} | ||
|
||
Defining the Domain Model | ||
-------------------------- | ||
|
||
Create a base class for common fields: | ||
|
||
.. code-block:: java | ||
|
||
public abstract class User { | ||
@Id | ||
private String id; | ||
private String name; | ||
private String email; | ||
private String userType; | ||
} | ||
|
||
Define subclasses for each user type: | ||
|
||
.. code-block:: java | ||
|
||
@Document(collection = "users") | ||
public class Customer extends User { | ||
private List<Order> orders; | ||
} | ||
|
||
.. code-block:: java | ||
|
||
@Document(collection = "users") | ||
public class Seller extends User { | ||
private List<Product> products; | ||
} | ||
|
||
.. code-block:: java | ||
|
||
@Document(collection = "users") | ||
public class Admin extends User { | ||
private List<String> permissions; | ||
} | ||
|
||
Implementing Repositories | ||
-------------------------- | ||
|
||
Create a repository interface for the ``User`` entity: | ||
|
||
.. code-block:: java | ||
|
||
public interface UserRepository extends MongoRepository<User, String> { | ||
List<User> findByUserType(String userType); | ||
} | ||
|
||
Service Layer Implementation | ||
---------------------------- | ||
|
||
Implement a service class to handle business logic: | ||
|
||
.. code-block:: java | ||
|
||
@Service | ||
public class UserService { | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
public List<User> getUsersByType(String userType) { | ||
return userRepository.findByUserType(userType); | ||
} | ||
} | ||
|
||
Testing the Implementation | ||
-------------------------- | ||
|
||
In your test class: | ||
|
||
.. code-block:: java | ||
|
||
@SpringBootTest | ||
public class UserServiceTests { | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
@Test | ||
public void testGetUsersByType() { | ||
List<User> customers = userService.getUsersByType("customer"); | ||
assertNotNull(customers); | ||
assertFalse(customers.isEmpty()); | ||
} | ||
} | ||
|
||
Benefits of Single-Collection Designs | ||
------------------------------------- | ||
|
||
- **Simplified Data Access**: All related data is stored in a single collection, reducing the need for joins. | ||
- **Improved Performance**: Fewer collections means fewer indexes to manage and faster queries in many use cases. | ||
- **Flexible Schema**: MongoDB’s schema-less nature enables storing different structures in the same collection. | ||
- **Easy Polymorphic Queries**: Query across all user types or filter by type using simple query parameters. | ||
|
||
Conclusion | ||
---------- | ||
|
||
Single-collection designs are a powerful modeling pattern in MongoDB that can simplify your application's persistence layer. | ||
Using Spring Data MongoDB in Java, you can implement this pattern with minimal configuration while maintaining strong typing and repository support. | ||
|
||
For more advanced scenarios—including handling polymorphism, aggregations, and validation—consider diving deeper into the Spring Data MongoDB documentation and the MongoDB schema design patterns. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
[Docs+] Single-Collection Designs in MongoDB with Spring Data Parts 1 and 2 #688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?