Skip to content

Commit a48609e

Browse files
Merge branch 'development' into docsp-54418-agg-fe
2 parents 34c74f2 + eceb07d commit a48609e

32 files changed

Lines changed: 2849 additions & 124 deletions

mflix/README-JAVA-SPRING.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Java Spring Boot MongoDB Sample MFlix Application
2+
3+
[TODO: Add intro]
4+
5+
```
6+
├── README.md
7+
├── client/ # Next.js frontend
8+
└── server/ # Java Spring backend
9+
```
10+
11+
## Getting Started
12+
13+
[TODO: Add getting started instructions explaining how to set connection string, start server, start client, etc.]
14+
15+
## Issues
16+
17+
If you have problems running the sample app, please check the following:
18+
19+
- [ ] Verify that you have set your MongoDB connection string in the `.env` file.
20+
- [ ] Verify that you have started the Java Spring server.
21+
- [ ] Verify that you have started the Next.js client.
22+
- [ ] Verify that you have no firewalls blocking access to the server or client ports.
23+
24+
If you have verified the above and still have issues, please
25+
[open an issue](https://github.com/mongodb/docs-sample-apps/issues/new/choose)
26+
on the source repository `mongodb/docs-sample-apps`.

mflix/README-JAVASCRIPT-EXPRESS.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# JavaScript Express.js MongoDB Sample MFlix Application
2+
3+
[TODO: Add intro]
4+
5+
```
6+
├── README.md
7+
├── client/ # Next.js frontend
8+
└── server/ # Express.js backend
9+
```
10+
11+
## Getting Started
12+
13+
[TODO: Add getting started instructions explaining how to set connection string, start server, start client, etc.]
14+
15+
## Issues
16+
17+
If you have problems running the sample app, please check the following:
18+
19+
- [ ] Verify that you have set your MongoDB connection string in the `.env` file.
20+
- [ ] Verify that you have started the Express server.
21+
- [ ] Verify that you have started the Next.js client.
22+
- [ ] Verify that you have no firewalls blocking access to the server or client ports.
23+
24+
If you have verified the above and still have issues, please
25+
[open an issue](https://github.com/mongodb/docs-sample-apps/issues/new/choose)
26+
on the source repository `mongodb/docs-sample-apps`.

mflix/README-PYTHON-FASTAPI.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Python FastAPI MongoDB Sample MFlix Application
2+
3+
[TODO: Add intro]
4+
5+
```
6+
├── README.md
7+
├── client/ # Next.js frontend
8+
└── server/ # Python FastAPI backend
9+
```
10+
11+
## Getting Started
12+
13+
[TODO: Add getting started instructions explaining how to set connection string, start server, start client, etc.]
14+
15+
## Issues
16+
17+
If you have problems running the sample app, please check the following:
18+
19+
- [ ] Verify that you have set your MongoDB connection string in the `.env` file.
20+
- [ ] Verify that you have started the Python FastAPI server.
21+
- [ ] Verify that you have started the Next.js client.
22+
- [ ] Verify that you have no firewalls blocking access to the server or client ports.
23+
24+
If you have verified the above and still have issues, please
25+
[open an issue](https://github.com/mongodb/docs-sample-apps/issues/new/choose)
26+
on the source repository `mongodb/docs-sample-apps`.

server/java-spring/README.md

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ This application provides a REST API for managing movie data from MongoDB's samp
88

99
- Spring Data MongoDB for simplified data access
1010
- CRUD operations (Create, Read, Update, Delete)
11-
- Text search functionality
11+
- MongoDB Atlas Search with multi-field search and compound operators
1212
- Filtering, sorting, and pagination
1313
- Comprehensive error handling
1414
- API documentation with Swagger/OpenAPI
15-
- MongoTemplate for complex queries
15+
- MongoTemplate for complex queries and aggregation pipelines
1616

1717
## Prerequisites
1818

@@ -104,6 +104,7 @@ Once the application is running, you can access:
104104

105105
### Movies (✅ Implemented)
106106

107+
#### CRUD Operations
107108
- `GET /api/movies` - Get all movies (with filtering, sorting, pagination)
108109
- `GET /api/movies/{id}` - Get a single movie by ID
109110
- `POST /api/movies` - Create a new movie
@@ -114,6 +115,48 @@ Once the application is running, you can access:
114115
- `DELETE /api/movies` - Delete multiple movies
115116
- `DELETE /api/movies/{id}/find-and-delete` - Find and delete a movie
116117

118+
#### Aggregations
119+
- `GET /api/movies/aggregations/comments` - Get movies with most comments
120+
- `GET /api/movies/aggregations/years` - Aggregate movies by year with statistics
121+
- `GET /api/movies/aggregations/directors` - Aggregate directors with most movies
122+
123+
#### Atlas Search
124+
- `GET /api/movies/search` - Search movies using MongoDB Atlas Search
125+
126+
**Query Parameters:**
127+
- `plot` (optional) - Search in plot field using phrase matching
128+
- `fullplot` (optional) - Search in fullplot field using phrase matching
129+
- `directors` (optional) - Search in directors field with fuzzy matching
130+
- `writers` (optional) - Search in writers field with fuzzy matching
131+
- `cast` (optional) - Search in cast field with fuzzy matching
132+
- `searchOperator` (optional) - Compound operator: `must` (default), `should`, `mustNot`, `filter`
133+
- `limit` (optional) - Maximum results to return (default: 20, max: 100)
134+
- `skip` (optional) - Number of results to skip for pagination (default: 0)
135+
136+
**Examples:**
137+
```bash
138+
# Search by plot
139+
GET /api/movies/search?plot=space+adventure
140+
141+
# Search by multiple fields with AND logic
142+
GET /api/movies/search?directors=Coppola&cast=Pacino&searchOperator=must
143+
144+
# Search by multiple fields with OR logic
145+
GET /api/movies/search?plot=crime&directors=Scorsese&searchOperator=should
146+
147+
# Search with pagination
148+
GET /api/movies/search?cast=Tom+Hanks&limit=10&skip=20
149+
```
150+
151+
**Note:** At least one search field must be provided. The `searchOperator` determines how multiple search criteria are combined:
152+
- `must` - All criteria must match (AND logic)
153+
- `should` - At least one criterion should match (OR logic)
154+
- `mustNot` - Criteria must not match (NOT logic)
155+
- `filter` - Criteria must match but don't affect scoring
156+
157+
#### Vector Search
158+
- `GET /api/movies/find-similar-movies` - Find similar movies using vector search on plot embeddings
159+
117160
## Development
118161

119162
### Running Tests
@@ -139,11 +182,19 @@ java -jar target/sample-mflix-spring-1.0.0.jar
139182

140183
- **Movies CRUD API** - Full create, read, update, delete operations
141184
- **Spring Data MongoDB** - Repository pattern with MongoTemplate for complex queries
142-
- **Text Search** - Full-text search on movie titles, plots, and genres
185+
- **MongoDB Atlas Search** - Multi-field search with compound operators (must, should, mustNot, filter)
186+
- Phrase matching on plot and fullplot fields
187+
- Fuzzy text matching on directors, writers, and cast fields
188+
- Support for complex search queries with multiple criteria
189+
- **MongoDB Aggregations** - Statistical aggregations by year, directors, and comments
190+
- **Vector Search** - Find similar movies using plot embeddings (requires Atlas Vector Search)
143191
- **Filtering & Pagination** - Query parameters for filtering, sorting, and pagination
144192
- **Custom Exception Handling** - Global exception handler with proper HTTP status codes
145193
- **Type-Safe DTOs** - Specific response types instead of generic Maps
146-
- **Unit Tests** - 35 tests covering service and controller layers
194+
- **Comprehensive Testing** - 60 tests covering service, controller, and integration layers
195+
- 29 controller unit tests
196+
- 27 service unit tests
197+
- 4 Atlas Search integration tests (requires Atlas cluster)
147198
- **OpenAPI Documentation** - Swagger UI available at `/swagger-ui.html`
148199
- **Database Verification** - Startup checks for database connectivity and indexes
149200

@@ -165,8 +216,15 @@ This application is designed as an educational sample to demonstrate:
165216
2. Best practices for Spring Boot REST API development
166217
3. Proper separation of concerns (Controller → Service → Repository)
167218
4. MongoDB CRUD operations and query patterns
168-
5. Error handling and validation in Spring Boot
169-
6. Using MongoTemplate for complex queries alongside Spring Data repositories
219+
5. **MongoDB Atlas Search** - Multi-field text search with compound operators
220+
- Phrase matching for exact phrase searches
221+
- Fuzzy text matching for typo-tolerant searches
222+
- Compound operators (must, should, mustNot, filter) for complex queries
223+
6. **MongoDB Aggregation Pipelines** - Statistical aggregations and data transformations
224+
7. **Vector Search** - Semantic similarity search using embeddings
225+
8. Error handling and validation in Spring Boot
226+
9. Using MongoTemplate for complex queries alongside Spring Data repositories
227+
10. Comprehensive testing strategies (unit tests, integration tests)
170228

171229
## Troubleshooting
172230

server/java-spring/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<artifactId>commons-lang3</artifactId>
6767
<version>${commons.lang3.version}</version>
6868
</dependency>
69+
6970
<!-- SpringDoc OpenAPI (Swagger UI) -->
7071
<dependency>
7172
<groupId>org.springdoc</groupId>
@@ -85,6 +86,12 @@
8586
<artifactId>spring-boot-starter-test</artifactId>
8687
<scope>test</scope>
8788
</dependency>
89+
90+
<!-- Jackson Databind for JSON serialization/deserialization -->
91+
<dependency>
92+
<groupId>com.fasterxml.jackson.core</groupId>
93+
<artifactId>jackson-databind</artifactId>
94+
</dependency>
8895
</dependencies>
8996

9097
<build>
@@ -103,6 +110,17 @@
103110
</configuration>
104111
</plugin>
105112

113+
<!-- Maven Compiler Plugin - Suppress annotation processing warnings -->
114+
<plugin>
115+
<groupId>org.apache.maven.plugins</groupId>
116+
<artifactId>maven-compiler-plugin</artifactId>
117+
<configuration>
118+
<compilerArgs>
119+
<arg>-Xlint:-options</arg>
120+
</compilerArgs>
121+
</configuration>
122+
</plugin>
123+
106124
<!-- Maven Surefire Plugin - Configure Mockito agent for Java 21+ -->
107125
<plugin>
108126
<groupId>org.apache.maven.plugins</groupId>
@@ -114,6 +132,7 @@
114132
</argLine>
115133
</configuration>
116134
</plugin>
135+
117136
<!-- Sort and remove unused imports -->
118137
<plugin>
119138
<groupId>net.revelc.code</groupId>

server/java-spring/src/main/java/com/mongodb/samplemflix/SampleMflixApplication.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mongodb.samplemflix;
22

3+
import io.swagger.v3.oas.annotations.Hidden;
34
import java.util.Map;
45
import org.springframework.boot.SpringApplication;
56
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -26,7 +27,9 @@ public static void main(String[] args) {
2627

2728
/**
2829
* Root endpoint providing basic information about the API.
30+
* Hidden from Swagger UI documentation.
2931
*/
32+
@Hidden
3033
@GetMapping("/")
3134
public Map<String, Object> root() {
3235
return Map.of(

server/java-spring/src/main/java/com/mongodb/samplemflix/config/CorsConfig.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.mongodb.samplemflix.config;
22

3-
import java.util.Arrays;
43
import org.springframework.beans.factory.annotation.Value;
54
import org.springframework.context.annotation.Bean;
65
import org.springframework.context.annotation.Configuration;
@@ -30,22 +29,29 @@ public class CorsConfig {
3029
@Bean
3130
public CorsFilter corsFilter() {
3231
CorsConfiguration config = new CorsConfiguration();
33-
34-
// Allow credentials (cookies, authorization headers)
35-
config.setAllowCredentials(true);
36-
37-
// Set allowed origins from environment variable
38-
config.setAllowedOrigins(Arrays.asList(allowedOrigins.split(",")));
39-
32+
33+
// Allow wildcard origins for Swagger UI
34+
config.setAllowCredentials(false);
35+
36+
// Set allowed origins - include localhost for Swagger UI
37+
// Parse the configured origins and add localhost variations for Swagger UI
38+
String[] origins = allowedOrigins.split(",");
39+
for (String origin : origins) {
40+
config.addAllowedOrigin(origin.trim());
41+
}
42+
// Add localhost origins for Swagger UI (on any port)
43+
config.addAllowedOriginPattern("http://localhost:*");
44+
config.addAllowedOriginPattern("http://127.0.0.1:*");
45+
4046
// Allow all headers
4147
config.addAllowedHeader("*");
42-
48+
4349
// Allow all HTTP methods
4450
config.addAllowedMethod("*");
45-
51+
4652
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
4753
source.registerCorsConfiguration("/**", config);
48-
54+
4955
return new CorsFilter(source);
5056
}
5157
}

0 commit comments

Comments
 (0)