Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
.idea/
target/
master_key.txt
.classpath
.project
*.prefs
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ To get started with MongoDB Atlas and get a free cluster read [this blog post](h

Add MongoDB Atlas Cluster URI in # src/main/resources/application.properties:
```
spring.data.mongodb.uri=mongodb+srv://<user>:<password>@<Cluster>>t/?retryWrites=true&w=majority
spring.data.mongodb.uri=mongodb+srv://<user>:<password>@<Cluster>/?retryWrites=true&w=majority
```

# Command lines
Expand All @@ -29,6 +29,8 @@ spring.data.mongodb.uri=mongodb+srv://<user>:<password>@<Cluster>>t/?retryWrites
mvn clean compile
```

## Session 1

- Run the `Create` class:

```sh
Expand Down Expand Up @@ -59,3 +61,43 @@ mvn spring-boot:run -Dspring-boot.run.arguments=delete
mvn spring-boot:run -Dspring-boot.run.arguments=mapping
```

## Session 2
For this session make sure to switch to the `sample_supplies` database in your `application.properties`.

- Run the `TotalSalesByLocationService` class:

```sh
mvn spring-boot:run -Dspring-boot.run.arguments=total-sales-by-location
```

- Run the `AverageCustomerSatisfactionService` class:

```sh
mvn spring-boot:run -Dspring-boot.run.arguments=average-customer-satisfaction
```

- Run the `AverageItemPricePerStoreService` class:
```sh
mvn spring-boot:run -Dspring-boot.run.arguments=average-item-price-per-store
```

- Run the `CountDistinctCustomersService` class:

```sh
mvn spring-boot:run -Dspring-boot.run.arguments=count-distinct-customers
```

- Run the `TotalSalesByDayOfWeekService` class:
```sh
mvn spring-boot:run -Dspring-boot.run.arguments=total-sales-by-day-of-week
```

- Run the `RevenueByLocationService` class:
```sh
mvn spring-boot:run -Dspring-boot.run.arguments=revenue-by-location
```

- Run the `SalesPerformanceService` class:
```sh
mvn spring-boot:run -Dspring-boot.run.arguments=sales-performance
```
30 changes: 16 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<maven-compiler-plugin.version>3.12.1</maven-compiler-plugin.version>
<mongodb-driver-sync.version>5.0.0</mongodb-driver-sync.version>
<mongodb-crypt.version>1.8.0</mongodb-crypt.version>
<spring-boot.version>3.2.0</spring-boot.version>
<spring-boot.version>3.2.5</spring-boot.version>
<exec-maven-plugin.version>3.1.1</exec-maven-plugin.version>
</properties>

Expand All @@ -32,31 +32,33 @@
<artifactId>mongodb-crypt</artifactId>
<version>${mongodb-crypt.version}</version>
</dependency>
<!-- Spring Boot Starter (Excluding Logging) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring Boot Starter Data MongoDB -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.14</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.13</version> <!-- Ensure version compatibility -->
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.mongodb.quickstart.Application</mainClass> <!-- Your main class -->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -73,7 +75,7 @@
<version>${exec-maven-plugin.version}</version>
<configuration>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
<mainClass>com.mongodb.quickstart.Application</mainClass> <!-- Specify your Application class -->
<mainClass>com.mongodb.quickstart.Application</mainClass> <!-- Your main class -->
</configuration>
</plugin>
</plugins>
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/mongodb/quickstart/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import com.mongodb.quickstart.services.AverageCustomerSatisfactionService;
import com.mongodb.quickstart.services.AverageItemPricePerStoreService;
import com.mongodb.quickstart.services.CountDistinctCustomersService;
import com.mongodb.quickstart.services.RevenueByLocationService;
import com.mongodb.quickstart.services.SalesPerformanceService;
import com.mongodb.quickstart.services.TotalSalesByDayOfWeekService;
import com.mongodb.quickstart.services.TotalSalesByLocationService;

@SpringBootApplication
@ComponentScan(basePackages = "com.mongodb.quickstart")
public class Application implements CommandLineRunner {
Expand Down Expand Up @@ -37,6 +45,27 @@ public void run(String... args) {
case "update":
context.getBean(Update.class).run();
break;
case "total-sales-by-location":
context.getBean(TotalSalesByLocationService.class).run();
break;
case "average-customer-satisfaction":
context.getBean(AverageCustomerSatisfactionService.class).run();
break;
case "average-item-price-per-store":
context.getBean(AverageItemPricePerStoreService.class).run();
break;
case "count-distinct-customers":
context.getBean(CountDistinctCustomersService.class).run();
break;
case "total-sales-by-day-of-week":
context.getBean(TotalSalesByDayOfWeekService.class).run();
break;
case "sales-performance":
context.getBean(SalesPerformanceService.class).run();
break;
case "revenue-by-location":
context.getBean(RevenueByLocationService.class).run();
break;
case "read":
default:
context.getBean(Read.class).run();
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/com/mongodb/quickstart/Delete.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.mongodb.quickstart.models.Grade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;

import org.springframework.stereotype.Component;

Expand All @@ -15,14 +13,14 @@ public class Delete {

public void run() {
// Delete one document
Grade grade = repository.findByStudentId(10000d);
Grade grade = repository.findFirstByStudentId(10000d);
if (grade != null) {
repository.delete(grade);
System.out.println("Deleted grade: " + grade);
}

// Find and delete one document
grade = repository.findByStudentId(10002d);
grade = repository.findFirstByStudentId(10002d);
if (grade != null) {
repository.delete(grade);
System.out.println("Deleted grade: " + grade);
Expand All @@ -32,8 +30,5 @@ public void run() {
repository.deleteAll(repository.findByStudentIdGreaterThanEqual(10000d));
System.out.println("Deleted all grades with student_id >= 10000.");

// Drop entire collection
repository.deleteAll();
System.out.println("Deleted the entire grades collection.");
}
}
4 changes: 1 addition & 3 deletions src/main/java/com/mongodb/quickstart/MappingPOJO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.mongodb.quickstart.models.Score;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
Expand All @@ -29,7 +27,7 @@ public void run() {
System.out.println("Grade inserted: " + newGrade);

// Find this grade
Grade grade = repository.findByStudentId(10003d);
Grade grade = repository.findFirstByStudentId(10003d);
System.out.println("Grade found: " + (grade != null ? grade : "No data found"));

// Update this grade by adding an exam grade
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/mongodb/quickstart/Read.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.mongodb.quickstart.models.Grade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
Expand All @@ -16,7 +15,7 @@ public class Read {
public void run() {
try {
// Find a grade by student ID
Grade grade = repository.findByStudentId(10000d);
Grade grade = repository.findFirstByStudentId(10000d);
System.out.println("Student 1 (via Repository): " + (grade != null ? grade : "No data found"));

// Find all grades with student IDs greater than or equal to 10000
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/mongodb/quickstart/StudentRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

@Repository
public interface StudentRepository extends MongoRepository<Grade, String> {
Grade findByStudentId(Double studentId);
Grade findFirstByStudentId(Double studentId);
List<Grade> findByStudentIdGreaterThanEqual(Double studentId);
List<Grade> findByStudentIdAndClassIdLessThanEqual(Double studentId, Double classId);
Grade findByStudentIdAndClassId(Double studentId, Double classId);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/mongodb/quickstart/Update.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Update {

public void run() {
// Update one document by adding a comment
Grade grade = repository.findByStudentId(10000d);
Grade grade = repository.findFirstByStudentId(10000d);
if (grade != null) {
Grade updatedGrade = repository.save(grade);
System.out.println("Grade updated: " + updatedGrade);
Expand All @@ -44,7 +44,7 @@ public void run() {
System.out.println("Updated all grades with student_id >= 10001.");

// Find and update
grade = repository.findByStudentId(10000d);
grade = repository.findFirstByStudentId(10000d);
if (grade != null) {
Grade updated = repository.save(grade);
System.out.println("Updated grade after finding: " + updated);
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/mongodb/quickstart/config/MongoConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.mongodb.quickstart.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;

@Configuration
public class MongoConfig extends AbstractMongoClientConfiguration {

@Value("${spring.data.mongodb.uri}")
private String mongoUri;

@Value("${spring.data.mongodb.database}")
private String databaseName;

@Override
protected String getDatabaseName() {
return databaseName;
}

@Bean
@Override
public MongoClient mongoClient() {
ConnectionString connectionString = new ConnectionString(mongoUri);
MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
.applicationName("devrel.springio.workshop.java")
.build();

return MongoClients.create(mongoClientSettings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mongodb.quickstart.dtos;

public record AverageItemPricePerStoreDTO(String _id, double averagePrice) {
public AverageItemPricePerStoreDTO(String _id, double averagePrice) {
this._id = _id;
this.averagePrice = averagePrice;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mongodb.quickstart.dtos;

public record CustomerSatisfactionDTO(String storeLocation, double averageSatisfaction) {
public CustomerSatisfactionDTO(String storeLocation, double averageSatisfaction) {
this.storeLocation = storeLocation;
this.averageSatisfaction = averageSatisfaction;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mongodb.quickstart.dtos;

public record DistinctCustomersCountDTO(String _id, int count) {
public DistinctCustomersCountDTO(String _id, int count) {
this._id = _id;
this.count = count;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mongodb.quickstart.dtos;

public record RevenueByLocationDTO(String storeLocation, double totalRevenue) {
public RevenueByLocationDTO(String storeLocation, double totalRevenue) {
this.storeLocation = storeLocation;
this.totalRevenue = totalRevenue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mongodb.quickstart.dtos;

public record SalesByDayOfWeekDTO(String _id, int totalSales) {
public SalesByDayOfWeekDTO(String _id, int totalSales) {
this._id = _id;
this.totalSales = totalSales;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.mongodb.quickstart.dtos;

public record SalesPerformanceDTO(String storeLocation, int salesWithCoupons, int salesWithoutCoupons) {
public SalesPerformanceDTO(String storeLocation, int salesWithCoupons, int salesWithoutCoupons) {
this.storeLocation = storeLocation;
this.salesWithCoupons = salesWithCoupons;
this.salesWithoutCoupons = salesWithoutCoupons;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mongodb.quickstart.dtos;

public record TotalSalesByLocationDTO(String _id, int totalSales) {
public TotalSalesByLocationDTO(String _id, int totalSales) {
this._id = _id;
this.totalSales = totalSales;
}
}
Loading