forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMongoReadAll.java
More file actions
32 lines (21 loc) · 857 Bytes
/
MongoReadAll.java
File metadata and controls
32 lines (21 loc) · 857 Bytes
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
package com.zetcode;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import org.bson.Document;
import java.util.ArrayList;
public class MongoReadAll {
public static void main(String[] args) {
try (var mongoClient = MongoClients.create("mongodb://localhost:27017")) {
var database = mongoClient.getDatabase("testdb");
MongoCollection<Document> collection = database.getCollection("cars");
try (MongoCursor<Document> cur = collection.find().iterator()) {
while (cur.hasNext()) {
var doc = cur.next();
var cars = new ArrayList<>(doc.values());
System.out.printf("%s: %s%n", cars.get(1), cars.get(2));
}
}
}
}
}