-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathHelloWorld.java
More file actions
50 lines (42 loc) · 2.13 KB
/
Copy pathHelloWorld.java
File metadata and controls
50 lines (42 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import static com.mongodb.client.model.Filters.eq;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
import com.mongodb.reactivestreams.client.MongoCollection;
import com.mongodb.reactivestreams.client.MongoDatabase;
import java.util.List;
import org.bson.Document;
import reactor.core.publisher.Mono;
public class HelloWorld {
// A few sample product documents seeded by this app so you can run
// it without loading an external dataset.
private static final List<Document> SAMPLE_PRODUCTS = List.of(
new Document("name", "Wireless Mouse")
.append("category", "Electronics")
.append("price", 24.99)
.append("tags", List.of("wireless", "usb", "ergonomic")),
new Document("name", "Standing Desk")
.append("category", "Furniture")
.append("price", 349.99)
.append("tags", List.of("adjustable", "office")),
new Document("name", "Noise-Cancelling Headphones")
.append("category", "Electronics")
.append("price", 199.99)
.append("tags", List.of("bluetooth", "wireless", "over-ear"))
);
public static void main(String[] args) {
String uri = System.getenv("MONGODB_URI");
try (MongoClient client = MongoClients.create(uri)) {
MongoDatabase database = client.getDatabase("get_started");
MongoCollection<Document> products = database.getCollection("products");
// Each reactive driver call returns a Publisher. Wrapping it in a
// Reactor Mono and calling block() runs the operation and waits for
// it to complete before moving on.
// Seed the collection so the app has data to query. Clearing the
// collection first keeps results consistent across repeated runs.
Mono.from(products.deleteMany(new Document())).block();
Mono.from(products.insertMany(SAMPLE_PRODUCTS)).block();
Document product = Mono.from(products.find(eq("name", "Wireless Mouse")).first()).block();
System.out.println(product.toJson());
}
}
}