Skip to content

Commit a21b946

Browse files
committed
DOCSP-33345: Java comments pt. 5 (#473)
* DOCSP-33345: Java comments pt. 5 * indicative mood * review feedback * sheet syntax * final feedback (cherry picked from commit e14a975)
1 parent 4fd0955 commit a21b946

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

source/includes/fundamentals/code-snippets/CurrentAPI.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class CurrentAPI {
1919

2020
public static void main(String[] args) throws InterruptedException {
2121
CurrentAPI c = new CurrentAPI();
22+
2223
c.example1();
2324
c.example2();
2425

@@ -29,13 +30,16 @@ private void example1() {
2930
MongoClient client = MongoClients.create(URI);
3031
MongoDatabase db = client.getDatabase(DATABASE);
3132
MongoCollection<Document> col = db.getCollection(COLLECTION);
33+
34+
// Prints the first document retrieved from the collection as JSON
3235
Document doc = col.find().first();
3336
System.out.println(doc.toJson());
3437
// end current-api-example
3538
client.close();
3639
}
3740

3841
private void example2() {
42+
// Creates a MongoClient that requests write acknowledgement from at least one node
3943
// start current-api-mongoclientsettings-example
4044
MongoClientSettings options = MongoClientSettings.builder()
4145
.applyConnectionString(new ConnectionString(URI))

source/includes/fundamentals/code-snippets/Cursor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,31 @@ public static void main(String [] args){
5959
}
6060

6161
private void forEachIteration(){
62+
// Prints the JSON representation of all documents in the collection
6263
// begin forEachIteration
6364
FindIterable<Document> iterable = collection.find();
6465
iterable.forEach(doc -> System.out.println(doc.toJson()));
6566
// end forEachIteration
6667
}
6768

6869
private void firstExample(){
70+
// Prints the first document that matches the query
6971
// begin firstExample
7072
FindIterable<Document> iterable = collection.find();
7173
System.out.println(iterable.first());
7274
// end firstExample
7375
}
7476

7577
private void availableExample(){
78+
// Prints the number of query results that are available in the returned cursor
7679
// begin availableExample
7780
MongoCursor<Document> cursor = collection.find().cursor();
7881
System.out.println(cursor.available());
7982
// end availableExample
8083
}
8184

8285
private void explainExample(){
86+
// Prints information about your find operation winning execution plan
8387
// begin explainExample
8488
Document explanation = collection.find().explain(ExplainVerbosity.EXECUTION_STATS);
8589
List<String> keys = Arrays.asList("queryPlanner", "winningPlan");
@@ -88,6 +92,7 @@ private void explainExample(){
8892
}
8993

9094
private void intoExample(){
95+
// Prints the results of the find operation as a list
9196
// begin intoExample
9297
List<Document> results = new ArrayList<>();
9398
FindIterable<Document> iterable = collection.find();
@@ -97,6 +102,7 @@ private void intoExample(){
97102
}
98103

99104
private void manualIteration(){
105+
// Prints the results of the find operation by iterating through a cursor
100106
// begin manualIteration
101107
MongoCursor<Document> cursor = collection.find().cursor();
102108
while (cursor.hasNext()){
@@ -106,6 +112,7 @@ private void manualIteration(){
106112
}
107113

108114
private void closeExample(){
115+
// Ensures the cursor frees up its resources after printing the documents it retrieved
109116
// begin closeExample
110117
MongoCursor<Document> cursor = collection.find().cursor();
111118

@@ -120,6 +127,7 @@ private void closeExample(){
120127
}
121128

122129
private void tryWithResourcesExample(){
130+
// Frees up a cursor's consumption of resources automatically with a try statement
123131
// begin tryWithResourcesExample
124132
try(MongoCursor<Document> cursor = collection.find().cursor()) {
125133
while (cursor.hasNext()){
@@ -131,6 +139,7 @@ private void tryWithResourcesExample(){
131139

132140
public void setupPaintCollection(){
133141
collection.drop();
142+
134143
collection.insertMany(Arrays.asList(
135144
new Document("_id", 1).append("color", "red").append("qty", 5),
136145
new Document("_id", 2).append("color", "purple").append("qty", 10),

source/includes/fundamentals/code-snippets/Delete.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,35 +52,41 @@ public static void main(String [] args){
5252
}
5353

5454
private void deleteManyExample(){
55+
// Deletes all documents in the collection that contain a "qty" value of 0
5556
// begin deleteManyExample
5657
Bson filter = Filters.eq("qty", 0);
5758
collection.deleteMany(filter);
5859
// end deleteManyExample
5960
}
6061

6162
private void findOneAndDeleteExample(){
63+
// Deletes the first document with a "color" value of "purple" and prints the deleted document
6264
// begin findOneAndDeleteExample
63-
Bson filter = Filters.eq("color", purple);
65+
Bson filter = Filters.eq("color", "purple");
6466
System.out.println(collection.findOneAndDelete(filter).toJson());
6567
// end findOneAndDeleteExample
6668
}
6769

6870
private void findOneAndDeleteNullExample(){
71+
// Deletes the first document with a "qty" value of 1 and prints the deleted document
6972
// begin findOneAndDeleteNullExample
7073
Bson filter = Filters.eq("qty", 1);
7174
System.out.println(collection.findOneAndDelete(filter));
7275
// end findOneAndDeleteNullExample
7376
}
7477

7578
private void deleteOneExample(){
79+
// Deletes the first document with a "color" value of "yellow"
7680
// begin deleteOneExample
7781
Bson filter = Filters.eq("color", "yellow");
7882
collection.deleteOne(filter);
7983
// end deleteOneExample
8084
}
8185
private void preview(boolean drop){
86+
// Prints all documents in a collection as JSON
8287
Bson filter = Filters.empty();
8388
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
89+
8490
if (drop){
8591
collection.drop();
8692
}

source/includes/fundamentals/code-snippets/Filters.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,34 +65,39 @@ public static void main(String[] args) {
6565
}
6666

6767
private void equalComparison() {
68+
// Prints all documents in a collection that have a "qty" value of "5" as JSON
6869
// begin equalComparison
6970
Bson equalComparison = eq("qty", 5);
7071
collection.find(equalComparison).forEach(doc -> System.out.println(doc.toJson()));
7172
// end equalComparison
7273
}
7374

7475
private void gteComparison() {
76+
// Prints all documents in a collection that have a "qty" value greater than "10" as JSON
7577
// begin gteComparison
7678
Bson gteComparison = gte("qty", 10);
7779
collection.find(gteComparison).forEach(doc -> System.out.println(doc.toJson()));
7880
// end gteComparison
7981
}
8082

8183
private void orComparison() {
84+
// Prints all documents in a collection that have a "qty" value of "8" or a "color" value of "pink" as JSON
8285
// begin orComparison
8386
Bson orComparison = or(gt("qty", 8), eq("color", "pink"));
8487
collection.find(orComparison).forEach(doc -> System.out.println(doc.toJson()));
8588
// end orComparison
8689
}
8790

8891
private void emptyComparison() {
92+
// Prints all documents in a collection as JSON
8993
// begin emptyComparison
9094
Bson emptyComparison = empty();
9195
collection.find(emptyComparison).forEach(doc -> System.out.println(doc.toJson()));
9296
// end emptyComparison
9397
}
9498

9599
private void allComparison() {
100+
// Prints all documents in which the "vendor" field contains all the elements of a list as JSON
96101
// begin allComparison
97102
List<String> search = Arrays.asList("A", "D");
98103
Bson allComparison = all("vendor", search);
@@ -101,20 +106,23 @@ private void allComparison() {
101106
}
102107

103108
private void existsComparison() {
109+
// Prints documents in which a "qty" field exists and the value is not "5" or "8" as JSON
104110
// begin existsComparison
105111
Bson existsComparison = and(exists("qty"), nin("qty", 5, 8));
106112
collection.find(existsComparison).forEach(doc -> System.out.println(doc.toJson()));
107113
// end existsComparison
108114
}
109115

110116
private void regexComparison() {
117+
// Prints all documents in which the "color" field value starts with "p" as JSON
111118
// begin regexComparison
112119
Bson regexComparison = regex("color", "^p");
113120
collection.find(regexComparison).forEach(doc -> System.out.println(doc.toJson()));
114121
// end regexComparison
115122
}
116123

117124
private void bitsComparison() {
125+
// Prints all documents that contain values in a field called "a" that match the bitmask value of "34"
118126
// begin bitsComparison
119127
Bson bitsComparison = bitsAllSet("a", 34);
120128
collection.find(bitsComparison).forEach(doc -> System.out.println(doc.toJson()));
@@ -129,6 +137,7 @@ private void geoWithinComparison() {
129137
new Position(0, 4),
130138
new Position(0, 0)));
131139

140+
// Prints documents that contain "coordinates" values that are within the bounds of the polygon passed as the filter parameter
132141
Bson geoWithinComparison = geoWithin("coordinates", square);
133142
collection.find(geoWithinComparison).forEach(doc -> System.out.println(doc.toJson()));
134143
// end geoWithinComparison
@@ -144,6 +153,7 @@ private void preview(){
144153
private void setupPaintCollection() {
145154

146155
List<Document> filterdata = new ArrayList<>();
156+
147157
String [] p1a = {"A"};
148158
String [] p2a = {"C", "D"};
149159
String [] p3a = {"B","A"};

source/includes/fundamentals/code-snippets/Geo.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,19 @@ public void go() {
4646
private void nearExample() {
4747
// begin findExample
4848

49-
// code to set up your mongo client ...
49+
// Add your MongoClient setup code here
5050
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
5151
MongoCollection<Document> collection = database.getCollection("theaters");
52+
5253
Point centralPark = new Point(new Position(-73.9667, 40.78));
54+
55+
// Creates a query that matches all locations between 5,000 and 10,000 meters from the specified Point
5356
Bson query = near("location.geo", centralPark, 10000.0, 5000.0);
57+
58+
// Creates a projection to include only the "location.address.city" field in the results
5459
Bson projection = fields(include("location.address.city"), excludeId());
60+
61+
// Prints the projected field of the results from the geospatial query as JSON
5562
collection.find(query)
5663
.projection(projection)
5764
.forEach(doc -> System.out.println(doc.toJson()));
@@ -62,14 +69,21 @@ private void rangeExample() {
6269
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
6370
MongoCollection<Document> collection = database.getCollection("theaters");
6471
// begin rangeExample
72+
// Add your MongoCollection setup code here
6573

66-
// code to set up your mongo collection ...
74+
// Creates a set of points that defines the bounds of a geospatial shape
6775
Polygon longIslandTriangle = new Polygon(Arrays.asList(new Position(-72, 40),
6876
new Position(-74, 41),
6977
new Position(-72, 39),
7078
new Position(-72, 40)));
79+
80+
// Creates a projection to include only the "location.address.city" field in the results
7181
Bson projection = fields(include("location.address.city"), excludeId());
82+
83+
// Creates a query that matches documents containing "location.geo" values within the specified bounds
7284
Bson geoWithinComparison = geoWithin("location.geo", longIslandTriangle);
85+
86+
// Prints the projected field of the results from the geolocation query as JSON
7387
collection.find(geoWithinComparison)
7488
.projection(projection)
7589
.forEach(doc -> System.out.println(doc.toJson()));

0 commit comments

Comments
 (0)