Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
This repo contains some simple Sprong-Boot Java program demonstrating using Oracle API for MongoDB

# License

Copyright (c) 2025 Oracle and/or its affiliates.

Licensed under the Universal Permissive License (UPL), Version 1.0.

See [LICENSE](https://github.com/oracle-devrel/technology-engineering/blob/main/LICENSE) for more details.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.oracle</groupId>
<artifactId>SpringBootDemo</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mongodb -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>3.5.4</version>
<exclusions>
<!-- avoid: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. on run with vscode -->
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>3.5.4</version>
<scope>test</scope>
<exclusions>
<!-- avoid: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. on run with vscode -->
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-testcontainers -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<version>3.5.4</version>
<scope>test</scope>
<exclusions>
<!-- avoid: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. on run with vscode -->
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testcontainers/testcontainers -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.21.3</version>
<scope>test</scope>
<exclusions>
<!-- avoid: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. on run with vscode -->
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.13.4</version>
<scope>test</scope>
<exclusions>
<!-- avoid: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. on run with vscode -->
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aaa
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

package com.oracle;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

@Document(collection = "colors")
public class Color {
@Id
private String objectId;

@Field(name = "name")
private String name;

public Color() {}

public Color(String id, String name) {
this.objectId = id;
this.name = name;
}

public void setObjectId(String id) {
this.objectId = id;
}

public String getObjectId() {
return this.objectId;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public String toString() {
return this.objectId+" : "+this.name;
}

@Override
public final boolean equals(Object o) {
if (!(o instanceof Color color)) return false;

return getObjectId().equals(color.getObjectId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.oracle;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

public interface ColorRepository extends MongoRepository<Color,String> {
@Query
Color findByName(String Name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.oracle;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
@SpringBootApplication
public class Main {

public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring:
data:
mongodb:
uri: ${DB_URI}
database: ${DB_NAME}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

package com.oracle;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.ReplaceOptions;
import org.bson.Document;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.bson.conversions.Bson;

@SpringBootTest
public class MongoTest {
@Autowired
ColorRepository colorRepository;

@Autowired
MongoTemplate mt;

Color blue = new Color("123","blue"),
grey = new Color("LU0010001-2000001891770-CAMT053-42","grey"),
green = new Color("001","green"),
yellow = new Color("002","yellow"),
red = new Color("003", "red");

@Test
void insertTest() {
try {
System.out.println("Save test");

System.out.println(colorRepository.save(blue));
System.out.println(colorRepository.save(grey));
System.out.println(colorRepository.save(green));
System.out.println(colorRepository.save(yellow));
System.out.println(colorRepository.save(red));

System.out.println("Save test done.");
}
catch(Exception e) {e.printStackTrace();}
}

@Test
void replaceTest() {
try {
System.out.println("Replace test");

MongoCollection<Document> col = mt.getCollection("colors");
Bson filter = Filters.eq("_id", "LU0010001-2000001891770-CAMT053-42");
ReplaceOptions replace = new ReplaceOptions().upsert(true);
Document d = new Document("_id", "LU0010001-2000001891770-CAMT053-42");
d.append("name", "white");
col.replaceOne(filter, d, replace);

System.out.println("End of reeplace test.");
}
catch (Exception e) {e.printStackTrace();}
}

@Test
void findTest() {
try {
System.out.println("Find test");
Color c = colorRepository.findByName("red");
System.out.println(c);
System.out.println("End of find test");
}
catch (Exception e) {e.printStackTrace();}
}
}