forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMongoProjection.java
More file actions
34 lines (21 loc) · 840 Bytes
/
MongoProjection.java
File metadata and controls
34 lines (21 loc) · 840 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
33
34
package com.zetcode;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import java.util.ArrayList;
import static com.mongodb.client.model.Projections.excludeId;
public class MongoProjection {
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");
FindIterable it = collection.find().projection(excludeId());
var docs = new ArrayList<Document>();
it.into(docs);
for (Document doc : docs) {
System.out.println(doc);
}
}
}
}