Skip to content

Commit 526e76d

Browse files
Merge pull request #1938 from oracle-devrel/witold-swierzy-patch-10
Witold swierzy patch 10
2 parents 5a0af33 + 466efdc commit 526e76d

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
This repo contains some simple Java console application which demonstrates how to connect to Oracle API for MongoDB instance and perform CRUD operations
2+
3+
# License
4+
5+
Copyright (c) 2025 Oracle and/or its affiliates.
6+
7+
Licensed under the Universal Permissive License (UPL), Version 1.0.
8+
9+
See [LICENSE](https://github.com/oracle-devrel/technology-engineering/blob/main/LICENSE) for more details.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.oracle;
2+
3+
import com.mongodb.client.MongoClient;
4+
import com.mongodb.client.MongoClients;
5+
import com.mongodb.client.MongoCollection;
6+
import com.mongodb.client.MongoDatabase;
7+
import com.mongodb.client.model.Filters;
8+
import com.mongodb.client.model.ReplaceOptions;
9+
import com.mongodb.client.model.UpdateOptions;
10+
import com.mongodb.client.model.Updates;
11+
import org.bson.Document;
12+
import org.bson.conversions.Bson;
13+
14+
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
15+
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
16+
public class Main {
17+
public static void main(String[] args) {
18+
MongoClient client;
19+
MongoDatabase db;
20+
String connectString = System.getenv("DB_URI");
21+
String dbName = System.getenv("DB_NAME");
22+
23+
try {
24+
client = MongoClients.create(connectString);
25+
db = client.getDatabase(dbName);
26+
27+
System.out.println("Connected!");
28+
29+
MongoCollection<Document> colors = db.getCollection("colors");
30+
colors.deleteMany(new Document());
31+
System.out.println("All documents form colors collection deleted.");
32+
33+
colors = db.getCollection("colors");
34+
Document d = new Document("name","black");
35+
d.append("_id","0001");
36+
colors.insertOne(d);
37+
d = new Document("name","yellow");
38+
colors.insertOne(d);
39+
d = new Document("name","green");
40+
colors.insertOne(d);
41+
System.out.println("Inserts done.");
42+
43+
Bson filter = Filters.eq("_id", "0001");
44+
Bson update = Updates.set("name","orange");
45+
UpdateOptions options = new UpdateOptions().upsert(true);
46+
System.out.println(colors.updateOne(filter,update,options));
47+
System.out.println("UPDATE with UPSERT done.");
48+
49+
ReplaceOptions replace = new ReplaceOptions().upsert(true);
50+
d = new Document("_id", "0001");
51+
d.append("name","white");
52+
colors.replaceOne(filter,d,replace);
53+
System.out.println("Replace done.");
54+
55+
System.out.println("Printing all documents from colors collection");
56+
Bson f = Filters.empty();
57+
colors.find(f).forEach(doc -> System.out.println(doc.toJson()));
58+
59+
}
60+
catch (Exception e) {e.printStackTrace();}
61+
}
62+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.oracle</groupId>
8+
<artifactId>BasicMongoDemo</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>21</maven.compiler.source>
13+
<maven.compiler.target>21</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.mongodb</groupId>
19+
<artifactId>mongodb-driver-sync</artifactId>
20+
<version>5.5.0</version>
21+
</dependency>
22+
<!-- https://mvnrepository.com/artifact/org.mongodb/bson -->
23+
<dependency>
24+
<groupId>org.mongodb</groupId>
25+
<artifactId>bson</artifactId>
26+
<version>5.5.1</version>
27+
</dependency>
28+
</dependencies>
29+
</project>

0 commit comments

Comments
 (0)