1 parent 298a5f0 commit 29508a2Copy full SHA for 29508a2
1 file changed
mongodb/MongoReadAll.java
@@ -0,0 +1,32 @@
1
+package com.zetcode;
2
+
3
+import com.mongodb.client.MongoClients;
4
+import com.mongodb.client.MongoCollection;
5
+import com.mongodb.client.MongoCursor;
6
+import org.bson.Document;
7
8
+import java.util.ArrayList;
9
10
+public class MongoReadAll {
11
12
+ public static void main(String[] args) {
13
14
+ try (var mongoClient = MongoClients.create("mongodb://localhost:27017")) {
15
16
+ var database = mongoClient.getDatabase("testdb");
17
18
+ MongoCollection<Document> collection = database.getCollection("cars");
19
20
+ try (MongoCursor<Document> cur = collection.find().iterator()) {
21
22
+ while (cur.hasNext()) {
23
24
+ var doc = cur.next();
25
+ var cars = new ArrayList<>(doc.values());
26
27
+ System.out.printf("%s: %s%n", cars.get(1), cars.get(2));
28
+ }
29
30
31
32
+}
0 commit comments