Skip to content

Commit 29508a2

Browse files
authored
Create MongoReadAll.java
1 parent 298a5f0 commit 29508a2

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

mongodb/MongoReadAll.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)