From 6f37512ee8363bb26074746ba556ceec09d42257 Mon Sep 17 00:00:00 2001 From: norareidy Date: Tue, 10 Oct 2023 16:14:52 -0400 Subject: [PATCH 1/5] DOCSP-33345: Java comments pt. 5 --- .../fundamentals/code-snippets/CurrentAPI.java | 6 ++++++ .../fundamentals/code-snippets/Cursor.java | 11 +++++++++++ .../fundamentals/code-snippets/Delete.java | 9 +++++++++ .../fundamentals/code-snippets/Filters.java | 18 ++++++++++++++++++ .../fundamentals/code-snippets/Geo.java | 18 ++++++++++++++++++ 5 files changed, 62 insertions(+) diff --git a/source/includes/fundamentals/code-snippets/CurrentAPI.java b/source/includes/fundamentals/code-snippets/CurrentAPI.java index 661b92b74..a18aff6f8 100644 --- a/source/includes/fundamentals/code-snippets/CurrentAPI.java +++ b/source/includes/fundamentals/code-snippets/CurrentAPI.java @@ -19,16 +19,21 @@ public class CurrentAPI { public static void main(String[] args) throws InterruptedException { CurrentAPI c = new CurrentAPI(); + + // Call methods on your CurrentAPI instance that demonstrate MongoDB operations c.example1(); c.example2(); } private void example1() { + // Connect to a MongoDB instance with the current API // start current-api-example MongoClient client = MongoClients.create(URI); MongoDatabase db = client.getDatabase(DATABASE); MongoCollection col = db.getCollection(COLLECTION); + + // Find and print a document in your collection to test your connection Document doc = col.find().first(); System.out.println(doc.toJson()); // end current-api-example @@ -36,6 +41,7 @@ private void example1() { } private void example2() { + // Set a write concern on your client with the current API // start current-api-mongoclientsettings-example MongoClientSettings options = MongoClientSettings.builder() .applyConnectionString(new ConnectionString(URI)) diff --git a/source/includes/fundamentals/code-snippets/Cursor.java b/source/includes/fundamentals/code-snippets/Cursor.java index a00c9e52b..e6a5bf8cb 100644 --- a/source/includes/fundamentals/code-snippets/Cursor.java +++ b/source/includes/fundamentals/code-snippets/Cursor.java @@ -30,6 +30,7 @@ private Cursor(){ } public static void main(String [] args){ + // Initialize a Cursor instance and run multiple find operations Cursor c = new Cursor(); c.setupPaintCollection(); @@ -59,6 +60,7 @@ public static void main(String [] args){ } private void forEachIteration(){ + // Iterate through your cursor results // begin forEachIteration FindIterable iterable = collection.find(); iterable.forEach(doc -> System.out.println(doc.toJson())); @@ -66,6 +68,7 @@ private void forEachIteration(){ } private void firstExample(){ + // Retrieve the first matching document from your query // begin firstExample FindIterable iterable = collection.find(); System.out.println(iterable.first()); @@ -73,6 +76,7 @@ private void firstExample(){ } private void availableExample(){ + // Retrieve the number of cursor results available locally without blocking // begin availableExample MongoCursor cursor = collection.find().cursor(); System.out.println(cursor.available()); @@ -80,6 +84,7 @@ private void availableExample(){ } private void explainExample(){ + // View performance information about your find operation execution // begin explainExample Document explanation = collection.find().explain(ExplainVerbosity.EXECUTION_STATS); List keys = Arrays.asList("queryPlanner", "winningPlan"); @@ -88,6 +93,7 @@ private void explainExample(){ } private void intoExample(){ + // Store your query results in a list // begin intoExample List results = new ArrayList<>(); FindIterable iterable = collection.find(); @@ -97,6 +103,7 @@ private void intoExample(){ } private void manualIteration(){ + // Continue your iteration only if more documents are available on the cursor // begin manualIteration MongoCursor cursor = collection.find().cursor(); while (cursor.hasNext()){ @@ -106,6 +113,7 @@ private void manualIteration(){ } private void closeExample(){ + // Free up a cursor's consumption of resources by calling close() // begin closeExample MongoCursor cursor = collection.find().cursor(); @@ -120,6 +128,7 @@ private void closeExample(){ } private void tryWithResourcesExample(){ + // Free up a cursor's consumption of resources automatically with a try statement // begin tryWithResourcesExample try(MongoCursor cursor = collection.find().cursor()) { while (cursor.hasNext()){ @@ -131,6 +140,8 @@ private void tryWithResourcesExample(){ public void setupPaintCollection(){ collection.drop(); + + // Insert sample documents into the "paint" collection collection.insertMany(Arrays.asList( new Document("_id", 1).append("color", "red").append("qty", 5), new Document("_id", 2).append("color", "purple").append("qty", 10), diff --git a/source/includes/fundamentals/code-snippets/Delete.java b/source/includes/fundamentals/code-snippets/Delete.java index a43f4b2e7..fbaaa3dba 100644 --- a/source/includes/fundamentals/code-snippets/Delete.java +++ b/source/includes/fundamentals/code-snippets/Delete.java @@ -28,6 +28,7 @@ private Delete() { } public static void main(String [] args){ + // Initialize a Delete instance and run multiple delete operations Delete delete = new Delete(); delete.preview(true); delete.setupPaintCollection(); @@ -52,6 +53,7 @@ public static void main(String [] args){ } private void deleteManyExample(){ + // Delete documents with a "qty" value of 0 // begin deleteManyExample Bson filter = Filters.eq("qty", 0); collection.deleteMany(filter); @@ -59,6 +61,7 @@ private void deleteManyExample(){ } private void findOneAndDeleteExample(){ + // Find and delete a document with a "color" value of purple, then print the document // begin findOneAndDeleteExample Bson filter = Filters.eq("color", purple); System.out.println(collection.findOneAndDelete(filter).toJson()); @@ -66,6 +69,7 @@ private void findOneAndDeleteExample(){ } private void findOneAndDeleteNullExample(){ + // Attempt to find and delete a nonexistant document, then print the result // begin findOneAndDeleteNullExample Bson filter = Filters.eq("qty", 1); System.out.println(collection.findOneAndDelete(filter)); @@ -73,14 +77,18 @@ private void findOneAndDeleteNullExample(){ } private void deleteOneExample(){ + // Delete a document with a "color" value of "yellow" // begin deleteOneExample Bson filter = Filters.eq("color", "yellow"); collection.deleteOne(filter); // end deleteOneExample } private void preview(boolean drop){ + // Print each document in your collection Bson filter = Filters.empty(); collection.find(filter).forEach(doc -> System.out.println(doc.toJson())); + + // Delete each document in your collection if the value of "drop" is "true" if (drop){ collection.drop(); } @@ -90,6 +98,7 @@ private void setupPaintCollection() { List deletedata = new ArrayList<>(); + // Insert sample documents into the "paint" collection Document p1 = new Document("_id", 1).append("color", "red").append("qty", 5); Document p2 = new Document("_id", 2).append("color", "purple").append("qty", 8); Document p3 = new Document("_id", 3).append("color", "blue").append("qty", 0); diff --git a/source/includes/fundamentals/code-snippets/Filters.java b/source/includes/fundamentals/code-snippets/Filters.java index d66624a1c..c6143723a 100644 --- a/source/includes/fundamentals/code-snippets/Filters.java +++ b/source/includes/fundamentals/code-snippets/Filters.java @@ -29,6 +29,7 @@ private Filters() { // begin declaration final String uri = System.getenv("DRIVER_REF_URI"); + // Create a client and access the "builders.filters" collection mongoClient = MongoClients.create(uri); database = mongoClient.getDatabase("builders"); collection = database.getCollection("filters"); @@ -41,6 +42,7 @@ public static void main(String[] args) { // filters.setupBinaryCollection(); // filters.setupPointsCollection(); + // Perform a series of find operations on your Filters instance System.out.println("Equal Comparison"); filters.equalComparison(); System.out.println("Empty Comparison"); @@ -65,6 +67,7 @@ public static void main(String[] args) { } private void equalComparison() { + // Query for documents with a "qty" field value of 5 and print them // begin equalComparison Bson equalComparison = eq("qty", 5); collection.find(equalComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -72,6 +75,7 @@ private void equalComparison() { } private void gteComparison() { + // Query for documents with a "qty" field value greater than 10 and print them // begin gteComparison Bson gteComparison = gte("qty", 10); collection.find(gteComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -79,6 +83,7 @@ private void gteComparison() { } private void orComparison() { + // Query for and print documents with a "qty" of 8 or a "color" of "pink" // begin orComparison Bson orComparison = or(gt("qty", 8), eq("color", "pink")); collection.find(orComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -86,6 +91,7 @@ private void orComparison() { } private void emptyComparison() { + // Use an empty filter to query for all documents // begin emptyComparison Bson emptyComparison = empty(); collection.find(emptyComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -93,6 +99,7 @@ private void emptyComparison() { } private void allComparison() { + // Query for documents where the "vendor" field contains all elements in "search" // begin allComparison List search = Arrays.asList("A", "D"); Bson allComparison = all("vendor", search); @@ -101,6 +108,7 @@ private void allComparison() { } private void existsComparison() { + // Query for documents where the "qty" field exists but is not 5 or 8 // begin existsComparison Bson existsComparison = and(exists("qty"), nin("qty", 5, 8)); collection.find(existsComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -108,6 +116,7 @@ private void existsComparison() { } private void regexComparison() { + // Query for documents where the "color" field value starts with "p" // begin regexComparison Bson regexComparison = regex("color", "^p"); collection.find(regexComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -115,6 +124,7 @@ private void regexComparison() { } private void bitsComparison() { + // Query for documents that have a "bitField" field with bits set at positions of the bitmask "34" // begin bitsComparison Bson bitsComparison = bitsAllSet("a", 34); collection.find(bitsComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -123,18 +133,21 @@ private void bitsComparison() { private void geoWithinComparison() { // begin geoWithinComparison + // Create a Polygon to filter your results Polygon square = new Polygon(Arrays.asList(new Position(0, 0), new Position(4, 0), new Position(4, 4), new Position(0, 4), new Position(0, 0))); + // Match documents with "coordinates" values that fall within the Polygon filter Bson geoWithinComparison = geoWithin("coordinates", square); collection.find(geoWithinComparison).forEach(doc -> System.out.println(doc.toJson())); // end geoWithinComparison } private void preview(){ + // Use an empty filter to print all documents in your collection Bson filter = new Document(); List res = new ArrayList(); System.out.println(collection.find(filter).into(res)); @@ -144,6 +157,8 @@ private void preview(){ private void setupPaintCollection() { List filterdata = new ArrayList<>(); + + // Set up the "vendor" field for your documents String [] p1a = {"A"}; String [] p2a = {"C", "D"}; String [] p3a = {"B","A"}; @@ -153,6 +168,7 @@ private void setupPaintCollection() { String [] p7a = {"B", "C"}; String [] p8a = {"A", "D"}; + // Create a series of documents and insert them into the "paint" collection Document p1 = new Document("_id", 1).append("color", "red").append("qty", 5).append("vendor", Arrays.asList(p1a)); Document p2 = new Document("_id", 2).append("color", "purple").append("qty", 10).append("vendor", Arrays.asList(p2a)); Document p3 = new Document("_id", 3).append("color", "blue").append("qty", 8).append("vendor", Arrays.asList(p3a)); @@ -178,6 +194,7 @@ private void setupPaintCollection() { private void setupBinaryCollection(){ List filterdata = new ArrayList<>(); + // Create a series of documents and insert them into the "binary" collection Document p9 = new Document("_id", 9).append("a", 54).append("binaryValue", "00110110"); Document p10 = new Document("_id", 10).append("a", 20).append("binaryValue", "00010100"); Document p11 = new Document("_id", 11).append("a", 68).append("binaryValue", "1000100"); @@ -194,6 +211,7 @@ private void setupPointsCollection(){ List filterdata = new ArrayList<>(); + // Create a series of documents and insert them into the "points" collection Document p13 = new Document("_id", 13).append("coordinates", new Point(new Position(2, 2))); Document p14 = new Document("_id", 14).append("coordinates", new Point(new Position(5, 6))); Document p15 = new Document("_id", 15).append("coordinates", new Point(new Position(1, 3))); diff --git a/source/includes/fundamentals/code-snippets/Geo.java b/source/includes/fundamentals/code-snippets/Geo.java index 4f31da2da..e65acf53e 100644 --- a/source/includes/fundamentals/code-snippets/Geo.java +++ b/source/includes/fundamentals/code-snippets/Geo.java @@ -35,6 +35,7 @@ public static void main(String[] args) { } public void go() { + // Create a Geo instance and run geospatial queries on your collection Geo geo = new Geo(); System.out.println("Near Example: "); geo.nearExample(); @@ -47,11 +48,20 @@ private void nearExample() { // begin findExample // code to set up your mongo client ... + // Access the "theaters" collection in the "sample_mflix" database MongoDatabase database = mongoClient.getDatabase("sample_mflix"); MongoCollection collection = database.getCollection("theaters"); + + // Store the coordinates of Central Park's Great Lawn in a Point instance Point centralPark = new Point(new Position(-73.9667, 40.78)); + + // Query for theaters betweeen 5,000 and 10,000 meters from the specified Point Bson query = near("location.geo", centralPark, 10000.0, 5000.0); + + // Specify a projection to project the documents' "location.address.city" field Bson projection = fields(include("location.address.city"), excludeId()); + + // Run the find operation with the specified query filter and projection collection.find(query) .projection(projection) .forEach(doc -> System.out.println(doc.toJson())); @@ -59,17 +69,25 @@ private void nearExample() { } private void rangeExample() { + // Access the "theaters" collection in the "sample_mflix" database MongoDatabase database = mongoClient.getDatabase("sample_mflix"); MongoCollection collection = database.getCollection("theaters"); // begin rangeExample // code to set up your mongo collection ... + // Create a Polygon instance to filter for locations within a section of Long Island Polygon longIslandTriangle = new Polygon(Arrays.asList(new Position(-72, 40), new Position(-74, 41), new Position(-72, 39), new Position(-72, 40))); + + // Specify a projection to project the documents' "location.address.city" field Bson projection = fields(include("location.address.city"), excludeId()); + + // Construct a query with the $geoWithin query operator Bson geoWithinComparison = geoWithin("location.geo", longIslandTriangle); + + // Run the find operation with the specified query filter and projection collection.find(geoWithinComparison) .projection(projection) .forEach(doc -> System.out.println(doc.toJson())); From 37d6011ed4eea0842056782f5c5c2e27b0e2849e Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 11 Oct 2023 17:35:34 -0400 Subject: [PATCH 2/5] indicative mood --- .../code-snippets/CurrentAPI.java | 7 ++-- .../fundamentals/code-snippets/Cursor.java | 20 ++++++------ .../fundamentals/code-snippets/Delete.java | 16 +++++----- .../fundamentals/code-snippets/Filters.java | 32 +++++++++---------- .../fundamentals/code-snippets/Geo.java | 22 ++++++------- 5 files changed, 48 insertions(+), 49 deletions(-) diff --git a/source/includes/fundamentals/code-snippets/CurrentAPI.java b/source/includes/fundamentals/code-snippets/CurrentAPI.java index a18aff6f8..e5172fb7a 100644 --- a/source/includes/fundamentals/code-snippets/CurrentAPI.java +++ b/source/includes/fundamentals/code-snippets/CurrentAPI.java @@ -20,20 +20,19 @@ public class CurrentAPI { public static void main(String[] args) throws InterruptedException { CurrentAPI c = new CurrentAPI(); - // Call methods on your CurrentAPI instance that demonstrate MongoDB operations c.example1(); c.example2(); } private void example1() { - // Connect to a MongoDB instance with the current API + // Connects to a MongoDB instance with the current API // start current-api-example MongoClient client = MongoClients.create(URI); MongoDatabase db = client.getDatabase(DATABASE); MongoCollection col = db.getCollection(COLLECTION); - // Find and print a document in your collection to test your connection + // Finds and prints a document in your collection Document doc = col.find().first(); System.out.println(doc.toJson()); // end current-api-example @@ -41,7 +40,7 @@ private void example1() { } private void example2() { - // Set a write concern on your client with the current API + // Sets a write concern on your client with the current API // start current-api-mongoclientsettings-example MongoClientSettings options = MongoClientSettings.builder() .applyConnectionString(new ConnectionString(URI)) diff --git a/source/includes/fundamentals/code-snippets/Cursor.java b/source/includes/fundamentals/code-snippets/Cursor.java index e6a5bf8cb..c3b947c02 100644 --- a/source/includes/fundamentals/code-snippets/Cursor.java +++ b/source/includes/fundamentals/code-snippets/Cursor.java @@ -30,7 +30,7 @@ private Cursor(){ } public static void main(String [] args){ - // Initialize a Cursor instance and run multiple find operations + // Initializes a Cursor instance and runs multiple find operations Cursor c = new Cursor(); c.setupPaintCollection(); @@ -60,7 +60,7 @@ public static void main(String [] args){ } private void forEachIteration(){ - // Iterate through your cursor results + // Iterates through your cursor results // begin forEachIteration FindIterable iterable = collection.find(); iterable.forEach(doc -> System.out.println(doc.toJson())); @@ -68,7 +68,7 @@ private void forEachIteration(){ } private void firstExample(){ - // Retrieve the first matching document from your query + // Retrieves the first matching document from your query // begin firstExample FindIterable iterable = collection.find(); System.out.println(iterable.first()); @@ -76,7 +76,7 @@ private void firstExample(){ } private void availableExample(){ - // Retrieve the number of cursor results available locally without blocking + // Retrieves the number of cursor results available locally without blocking // begin availableExample MongoCursor cursor = collection.find().cursor(); System.out.println(cursor.available()); @@ -84,7 +84,7 @@ private void availableExample(){ } private void explainExample(){ - // View performance information about your find operation execution + // Returns performance information about your find operation execution // begin explainExample Document explanation = collection.find().explain(ExplainVerbosity.EXECUTION_STATS); List keys = Arrays.asList("queryPlanner", "winningPlan"); @@ -93,7 +93,7 @@ private void explainExample(){ } private void intoExample(){ - // Store your query results in a list + // Stores your query results in a list // begin intoExample List results = new ArrayList<>(); FindIterable iterable = collection.find(); @@ -103,7 +103,7 @@ private void intoExample(){ } private void manualIteration(){ - // Continue your iteration only if more documents are available on the cursor + // Continues your iteration only if more documents are available on the cursor // begin manualIteration MongoCursor cursor = collection.find().cursor(); while (cursor.hasNext()){ @@ -113,7 +113,7 @@ private void manualIteration(){ } private void closeExample(){ - // Free up a cursor's consumption of resources by calling close() + // Frees up a cursor's consumption of resources by calling close() // begin closeExample MongoCursor cursor = collection.find().cursor(); @@ -128,7 +128,7 @@ private void closeExample(){ } private void tryWithResourcesExample(){ - // Free up a cursor's consumption of resources automatically with a try statement + // Frees up a cursor's consumption of resources automatically with a try statement // begin tryWithResourcesExample try(MongoCursor cursor = collection.find().cursor()) { while (cursor.hasNext()){ @@ -141,7 +141,7 @@ private void tryWithResourcesExample(){ public void setupPaintCollection(){ collection.drop(); - // Insert sample documents into the "paint" collection + // Inserts sample documents into the "paint" collection collection.insertMany(Arrays.asList( new Document("_id", 1).append("color", "red").append("qty", 5), new Document("_id", 2).append("color", "purple").append("qty", 10), diff --git a/source/includes/fundamentals/code-snippets/Delete.java b/source/includes/fundamentals/code-snippets/Delete.java index fbaaa3dba..2602feef2 100644 --- a/source/includes/fundamentals/code-snippets/Delete.java +++ b/source/includes/fundamentals/code-snippets/Delete.java @@ -28,7 +28,7 @@ private Delete() { } public static void main(String [] args){ - // Initialize a Delete instance and run multiple delete operations + // Initializes a Delete instance and run multiple delete operations Delete delete = new Delete(); delete.preview(true); delete.setupPaintCollection(); @@ -53,7 +53,7 @@ public static void main(String [] args){ } private void deleteManyExample(){ - // Delete documents with a "qty" value of 0 + // Deletes documents with a "qty" value of 0 // begin deleteManyExample Bson filter = Filters.eq("qty", 0); collection.deleteMany(filter); @@ -61,7 +61,7 @@ private void deleteManyExample(){ } private void findOneAndDeleteExample(){ - // Find and delete a document with a "color" value of purple, then print the document + // Finds and deletes a document with a "color" value of purple, then prints the document // begin findOneAndDeleteExample Bson filter = Filters.eq("color", purple); System.out.println(collection.findOneAndDelete(filter).toJson()); @@ -69,7 +69,7 @@ private void findOneAndDeleteExample(){ } private void findOneAndDeleteNullExample(){ - // Attempt to find and delete a nonexistant document, then print the result + // Attempts to find and delete a nonexistent document, then prints the result // begin findOneAndDeleteNullExample Bson filter = Filters.eq("qty", 1); System.out.println(collection.findOneAndDelete(filter)); @@ -77,18 +77,18 @@ private void findOneAndDeleteNullExample(){ } private void deleteOneExample(){ - // Delete a document with a "color" value of "yellow" + // Deletes a document with a "color" value of "yellow" // begin deleteOneExample Bson filter = Filters.eq("color", "yellow"); collection.deleteOne(filter); // end deleteOneExample } private void preview(boolean drop){ - // Print each document in your collection + // Prints each document in your collection Bson filter = Filters.empty(); collection.find(filter).forEach(doc -> System.out.println(doc.toJson())); - // Delete each document in your collection if the value of "drop" is "true" + // Deletes each document in your collection if the value of "drop" is "true" if (drop){ collection.drop(); } @@ -98,7 +98,7 @@ private void setupPaintCollection() { List deletedata = new ArrayList<>(); - // Insert sample documents into the "paint" collection + // Inserts sample documents into the "paint" collection Document p1 = new Document("_id", 1).append("color", "red").append("qty", 5); Document p2 = new Document("_id", 2).append("color", "purple").append("qty", 8); Document p3 = new Document("_id", 3).append("color", "blue").append("qty", 0); diff --git a/source/includes/fundamentals/code-snippets/Filters.java b/source/includes/fundamentals/code-snippets/Filters.java index c6143723a..b73b46237 100644 --- a/source/includes/fundamentals/code-snippets/Filters.java +++ b/source/includes/fundamentals/code-snippets/Filters.java @@ -29,7 +29,7 @@ private Filters() { // begin declaration final String uri = System.getenv("DRIVER_REF_URI"); - // Create a client and access the "builders.filters" collection + // Creates a client and accesses the "builders.filters" collection mongoClient = MongoClients.create(uri); database = mongoClient.getDatabase("builders"); collection = database.getCollection("filters"); @@ -42,7 +42,7 @@ public static void main(String[] args) { // filters.setupBinaryCollection(); // filters.setupPointsCollection(); - // Perform a series of find operations on your Filters instance + // Performs a series of find operations on your Filters instance System.out.println("Equal Comparison"); filters.equalComparison(); System.out.println("Empty Comparison"); @@ -67,7 +67,7 @@ public static void main(String[] args) { } private void equalComparison() { - // Query for documents with a "qty" field value of 5 and print them + // Queries for documents with a "qty" field value of 5 and prints them // begin equalComparison Bson equalComparison = eq("qty", 5); collection.find(equalComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -75,7 +75,7 @@ private void equalComparison() { } private void gteComparison() { - // Query for documents with a "qty" field value greater than 10 and print them + // Queries for documents with a "qty" field value greater than 10 and prints them // begin gteComparison Bson gteComparison = gte("qty", 10); collection.find(gteComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -83,7 +83,7 @@ private void gteComparison() { } private void orComparison() { - // Query for and print documents with a "qty" of 8 or a "color" of "pink" + // Queries for and prints documents with a "qty" of 8 or a "color" of "pink" // begin orComparison Bson orComparison = or(gt("qty", 8), eq("color", "pink")); collection.find(orComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -91,7 +91,7 @@ private void orComparison() { } private void emptyComparison() { - // Use an empty filter to query for all documents + // Uses an empty filter to query for all documents // begin emptyComparison Bson emptyComparison = empty(); collection.find(emptyComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -99,7 +99,7 @@ private void emptyComparison() { } private void allComparison() { - // Query for documents where the "vendor" field contains all elements in "search" + // Queries for documents where the "vendor" field contains all elements in "search" // begin allComparison List search = Arrays.asList("A", "D"); Bson allComparison = all("vendor", search); @@ -108,7 +108,7 @@ private void allComparison() { } private void existsComparison() { - // Query for documents where the "qty" field exists but is not 5 or 8 + // Queries for documents where the "qty" field exists but is not 5 or 8 // begin existsComparison Bson existsComparison = and(exists("qty"), nin("qty", 5, 8)); collection.find(existsComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -116,7 +116,7 @@ private void existsComparison() { } private void regexComparison() { - // Query for documents where the "color" field value starts with "p" + // Queries for documents where the "color" field value starts with "p" // begin regexComparison Bson regexComparison = regex("color", "^p"); collection.find(regexComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -124,7 +124,7 @@ private void regexComparison() { } private void bitsComparison() { - // Query for documents that have a "bitField" field with bits set at positions of the bitmask "34" + // Queries for documents that have a "bitField" field with bits set at the bitmask "34" positions // begin bitsComparison Bson bitsComparison = bitsAllSet("a", 34); collection.find(bitsComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -133,21 +133,21 @@ private void bitsComparison() { private void geoWithinComparison() { // begin geoWithinComparison - // Create a Polygon to filter your results + // Creates a Polygon to filter the results Polygon square = new Polygon(Arrays.asList(new Position(0, 0), new Position(4, 0), new Position(4, 4), new Position(0, 4), new Position(0, 0))); - // Match documents with "coordinates" values that fall within the Polygon filter + // Matches documents with "coordinates" values that fall within the Polygon filter Bson geoWithinComparison = geoWithin("coordinates", square); collection.find(geoWithinComparison).forEach(doc -> System.out.println(doc.toJson())); // end geoWithinComparison } private void preview(){ - // Use an empty filter to print all documents in your collection + // Uses an empty filter to print all documents in the collection Bson filter = new Document(); List res = new ArrayList(); System.out.println(collection.find(filter).into(res)); @@ -168,7 +168,7 @@ private void setupPaintCollection() { String [] p7a = {"B", "C"}; String [] p8a = {"A", "D"}; - // Create a series of documents and insert them into the "paint" collection + // Creates a series of documents and inserts them into the "paint" collection Document p1 = new Document("_id", 1).append("color", "red").append("qty", 5).append("vendor", Arrays.asList(p1a)); Document p2 = new Document("_id", 2).append("color", "purple").append("qty", 10).append("vendor", Arrays.asList(p2a)); Document p3 = new Document("_id", 3).append("color", "blue").append("qty", 8).append("vendor", Arrays.asList(p3a)); @@ -194,7 +194,7 @@ private void setupPaintCollection() { private void setupBinaryCollection(){ List filterdata = new ArrayList<>(); - // Create a series of documents and insert them into the "binary" collection + // Creates a series of documents and inserts them into the "binary" collection Document p9 = new Document("_id", 9).append("a", 54).append("binaryValue", "00110110"); Document p10 = new Document("_id", 10).append("a", 20).append("binaryValue", "00010100"); Document p11 = new Document("_id", 11).append("a", 68).append("binaryValue", "1000100"); @@ -211,7 +211,7 @@ private void setupPointsCollection(){ List filterdata = new ArrayList<>(); - // Create a series of documents and insert them into the "points" collection + // Creates a series of documents and inserts them into the "points" collection Document p13 = new Document("_id", 13).append("coordinates", new Point(new Position(2, 2))); Document p14 = new Document("_id", 14).append("coordinates", new Point(new Position(5, 6))); Document p15 = new Document("_id", 15).append("coordinates", new Point(new Position(1, 3))); diff --git a/source/includes/fundamentals/code-snippets/Geo.java b/source/includes/fundamentals/code-snippets/Geo.java index e65acf53e..cccbc68ce 100644 --- a/source/includes/fundamentals/code-snippets/Geo.java +++ b/source/includes/fundamentals/code-snippets/Geo.java @@ -35,7 +35,7 @@ public static void main(String[] args) { } public void go() { - // Create a Geo instance and run geospatial queries on your collection + // Creates a Geo instance and runs geospatial queries on your collection Geo geo = new Geo(); System.out.println("Near Example: "); geo.nearExample(); @@ -48,20 +48,20 @@ private void nearExample() { // begin findExample // code to set up your mongo client ... - // Access the "theaters" collection in the "sample_mflix" database + // Accesses the "theaters" collection in the "sample_mflix" database MongoDatabase database = mongoClient.getDatabase("sample_mflix"); MongoCollection collection = database.getCollection("theaters"); - // Store the coordinates of Central Park's Great Lawn in a Point instance + // Stores the coordinates of Central Park's Great Lawn in a Point instance Point centralPark = new Point(new Position(-73.9667, 40.78)); - // Query for theaters betweeen 5,000 and 10,000 meters from the specified Point + // Queries for theaters between 5,000 and 10,000 meters from the specified Point Bson query = near("location.geo", centralPark, 10000.0, 5000.0); - // Specify a projection to project the documents' "location.address.city" field + // Specifies a projection to project the documents' "location.address.city" field Bson projection = fields(include("location.address.city"), excludeId()); - // Run the find operation with the specified query filter and projection + // Runs the find operation with the specified query filter and projection collection.find(query) .projection(projection) .forEach(doc -> System.out.println(doc.toJson())); @@ -69,25 +69,25 @@ private void nearExample() { } private void rangeExample() { - // Access the "theaters" collection in the "sample_mflix" database + // Accesses the "theaters" collection in the "sample_mflix" database MongoDatabase database = mongoClient.getDatabase("sample_mflix"); MongoCollection collection = database.getCollection("theaters"); // begin rangeExample // code to set up your mongo collection ... - // Create a Polygon instance to filter for locations within a section of Long Island + // Creates a Polygon instance to filter for locations within a section of Long Island Polygon longIslandTriangle = new Polygon(Arrays.asList(new Position(-72, 40), new Position(-74, 41), new Position(-72, 39), new Position(-72, 40))); - // Specify a projection to project the documents' "location.address.city" field + // Specifies a projection to project the documents' "location.address.city" field Bson projection = fields(include("location.address.city"), excludeId()); - // Construct a query with the $geoWithin query operator + // Constructs a query with the $geoWithin query operator Bson geoWithinComparison = geoWithin("location.geo", longIslandTriangle); - // Run the find operation with the specified query filter and projection + // Runs the find operation with the specified query filter and projection collection.find(geoWithinComparison) .projection(projection) .forEach(doc -> System.out.println(doc.toJson())); From 2ab966be1683671b70a0c2fa4af82f6f677233a8 Mon Sep 17 00:00:00 2001 From: norareidy Date: Mon, 16 Oct 2023 11:10:34 -0400 Subject: [PATCH 3/5] review feedback --- .../code-snippets/CurrentAPI.java | 6 ++--- .../fundamentals/code-snippets/Cursor.java | 16 +++++------- .../fundamentals/code-snippets/Delete.java | 11 +++----- .../fundamentals/code-snippets/Filters.java | 26 +++++++------------ .../fundamentals/code-snippets/Geo.java | 20 ++++++-------- 5 files changed, 31 insertions(+), 48 deletions(-) diff --git a/source/includes/fundamentals/code-snippets/CurrentAPI.java b/source/includes/fundamentals/code-snippets/CurrentAPI.java index e5172fb7a..f14ba8ac8 100644 --- a/source/includes/fundamentals/code-snippets/CurrentAPI.java +++ b/source/includes/fundamentals/code-snippets/CurrentAPI.java @@ -26,13 +26,13 @@ public static void main(String[] args) throws InterruptedException { } private void example1() { - // Connects to a MongoDB instance with the current API + // Connects to MongoDB and retrieves a document by using the non-legacy API // start current-api-example MongoClient client = MongoClients.create(URI); MongoDatabase db = client.getDatabase(DATABASE); MongoCollection col = db.getCollection(COLLECTION); - // Finds and prints a document in your collection + // Prints the first document retrieved from the collection in JSON format Document doc = col.find().first(); System.out.println(doc.toJson()); // end current-api-example @@ -40,7 +40,7 @@ private void example1() { } private void example2() { - // Sets a write concern on your client with the current API + // Creates a MongoClient that requests write acknowledgement from at least one node // start current-api-mongoclientsettings-example MongoClientSettings options = MongoClientSettings.builder() .applyConnectionString(new ConnectionString(URI)) diff --git a/source/includes/fundamentals/code-snippets/Cursor.java b/source/includes/fundamentals/code-snippets/Cursor.java index c3b947c02..57f9df612 100644 --- a/source/includes/fundamentals/code-snippets/Cursor.java +++ b/source/includes/fundamentals/code-snippets/Cursor.java @@ -30,7 +30,6 @@ private Cursor(){ } public static void main(String [] args){ - // Initializes a Cursor instance and runs multiple find operations Cursor c = new Cursor(); c.setupPaintCollection(); @@ -60,7 +59,7 @@ public static void main(String [] args){ } private void forEachIteration(){ - // Iterates through your cursor results + // Prints all the document in the collection in JSON format // begin forEachIteration FindIterable iterable = collection.find(); iterable.forEach(doc -> System.out.println(doc.toJson())); @@ -68,7 +67,7 @@ private void forEachIteration(){ } private void firstExample(){ - // Retrieves the first matching document from your query + // Prints the first document that matches the query // begin firstExample FindIterable iterable = collection.find(); System.out.println(iterable.first()); @@ -76,7 +75,7 @@ private void firstExample(){ } private void availableExample(){ - // Retrieves the number of cursor results available locally without blocking + // Prints the number of query results that available in the returned cursor // begin availableExample MongoCursor cursor = collection.find().cursor(); System.out.println(cursor.available()); @@ -84,7 +83,7 @@ private void availableExample(){ } private void explainExample(){ - // Returns performance information about your find operation execution + // Prints information about your find operation winning execution plan // begin explainExample Document explanation = collection.find().explain(ExplainVerbosity.EXECUTION_STATS); List keys = Arrays.asList("queryPlanner", "winningPlan"); @@ -93,7 +92,7 @@ private void explainExample(){ } private void intoExample(){ - // Stores your query results in a list + // Prints the query results // begin intoExample List results = new ArrayList<>(); FindIterable iterable = collection.find(); @@ -103,7 +102,7 @@ private void intoExample(){ } private void manualIteration(){ - // Continues your iteration only if more documents are available on the cursor + // Iterates through cursor results of a query and prints them as JSON // begin manualIteration MongoCursor cursor = collection.find().cursor(); while (cursor.hasNext()){ @@ -113,7 +112,7 @@ private void manualIteration(){ } private void closeExample(){ - // Frees up a cursor's consumption of resources by calling close() + // Ensures the cursor frees up its resources after printing the documents it retrieved // begin closeExample MongoCursor cursor = collection.find().cursor(); @@ -141,7 +140,6 @@ private void tryWithResourcesExample(){ public void setupPaintCollection(){ collection.drop(); - // Inserts sample documents into the "paint" collection collection.insertMany(Arrays.asList( new Document("_id", 1).append("color", "red").append("qty", 5), new Document("_id", 2).append("color", "purple").append("qty", 10), diff --git a/source/includes/fundamentals/code-snippets/Delete.java b/source/includes/fundamentals/code-snippets/Delete.java index 2602feef2..208cafb5b 100644 --- a/source/includes/fundamentals/code-snippets/Delete.java +++ b/source/includes/fundamentals/code-snippets/Delete.java @@ -28,7 +28,6 @@ private Delete() { } public static void main(String [] args){ - // Initializes a Delete instance and run multiple delete operations Delete delete = new Delete(); delete.preview(true); delete.setupPaintCollection(); @@ -53,7 +52,7 @@ public static void main(String [] args){ } private void deleteManyExample(){ - // Deletes documents with a "qty" value of 0 + // Deletes all documents in the collection that contain a "qty" value of 0 // begin deleteManyExample Bson filter = Filters.eq("qty", 0); collection.deleteMany(filter); @@ -61,7 +60,7 @@ private void deleteManyExample(){ } private void findOneAndDeleteExample(){ - // Finds and deletes a document with a "color" value of purple, then prints the document + // Deletes the first document with a "color" value of "purple", and prints the deleted document // begin findOneAndDeleteExample Bson filter = Filters.eq("color", purple); System.out.println(collection.findOneAndDelete(filter).toJson()); @@ -69,7 +68,7 @@ private void findOneAndDeleteExample(){ } private void findOneAndDeleteNullExample(){ - // Attempts to find and delete a nonexistent document, then prints the result + // Deletes the first document with a "qty" value of 1, and prints the deleted document // begin findOneAndDeleteNullExample Bson filter = Filters.eq("qty", 1); System.out.println(collection.findOneAndDelete(filter)); @@ -77,7 +76,7 @@ private void findOneAndDeleteNullExample(){ } private void deleteOneExample(){ - // Deletes a document with a "color" value of "yellow" + // Deletes the first document with a "color" value of "yellow" // begin deleteOneExample Bson filter = Filters.eq("color", "yellow"); collection.deleteOne(filter); @@ -88,7 +87,6 @@ private void preview(boolean drop){ Bson filter = Filters.empty(); collection.find(filter).forEach(doc -> System.out.println(doc.toJson())); - // Deletes each document in your collection if the value of "drop" is "true" if (drop){ collection.drop(); } @@ -98,7 +96,6 @@ private void setupPaintCollection() { List deletedata = new ArrayList<>(); - // Inserts sample documents into the "paint" collection Document p1 = new Document("_id", 1).append("color", "red").append("qty", 5); Document p2 = new Document("_id", 2).append("color", "purple").append("qty", 8); Document p3 = new Document("_id", 3).append("color", "blue").append("qty", 0); diff --git a/source/includes/fundamentals/code-snippets/Filters.java b/source/includes/fundamentals/code-snippets/Filters.java index b73b46237..96c7440a1 100644 --- a/source/includes/fundamentals/code-snippets/Filters.java +++ b/source/includes/fundamentals/code-snippets/Filters.java @@ -29,7 +29,6 @@ private Filters() { // begin declaration final String uri = System.getenv("DRIVER_REF_URI"); - // Creates a client and accesses the "builders.filters" collection mongoClient = MongoClients.create(uri); database = mongoClient.getDatabase("builders"); collection = database.getCollection("filters"); @@ -42,7 +41,6 @@ public static void main(String[] args) { // filters.setupBinaryCollection(); // filters.setupPointsCollection(); - // Performs a series of find operations on your Filters instance System.out.println("Equal Comparison"); filters.equalComparison(); System.out.println("Empty Comparison"); @@ -67,7 +65,7 @@ public static void main(String[] args) { } private void equalComparison() { - // Queries for documents with a "qty" field value of 5 and prints them + // Prints all documents in a collection that contain a "qty" value of "5" // begin equalComparison Bson equalComparison = eq("qty", 5); collection.find(equalComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -75,7 +73,7 @@ private void equalComparison() { } private void gteComparison() { - // Queries for documents with a "qty" field value greater than 10 and prints them + // Prints all documents in a collection with a "qty" field value greater than "10" // begin gteComparison Bson gteComparison = gte("qty", 10); collection.find(gteComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -83,7 +81,7 @@ private void gteComparison() { } private void orComparison() { - // Queries for and prints documents with a "qty" of 8 or a "color" of "pink" + // Prints all documents in a collection with a "qty" value of "8" or a "color" value of "pink" // begin orComparison Bson orComparison = or(gt("qty", 8), eq("color", "pink")); collection.find(orComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -91,7 +89,7 @@ private void orComparison() { } private void emptyComparison() { - // Uses an empty filter to query for all documents + // Prints all documents in a collection // begin emptyComparison Bson emptyComparison = empty(); collection.find(emptyComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -99,7 +97,7 @@ private void emptyComparison() { } private void allComparison() { - // Queries for documents where the "vendor" field contains all elements in "search" + // Prints all documents in which the "vendor" field contains all the elements of a list // begin allComparison List search = Arrays.asList("A", "D"); Bson allComparison = all("vendor", search); @@ -108,7 +106,7 @@ private void allComparison() { } private void existsComparison() { - // Queries for documents where the "qty" field exists but is not 5 or 8 + // Prints documents in which a "qty" field exists and the value is not "5" or "8" // begin existsComparison Bson existsComparison = and(exists("qty"), nin("qty", 5, 8)); collection.find(existsComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -116,7 +114,7 @@ private void existsComparison() { } private void regexComparison() { - // Queries for documents where the "color" field value starts with "p" + // Prints all documents in which the "color" field value starts with "p" // begin regexComparison Bson regexComparison = regex("color", "^p"); collection.find(regexComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -124,7 +122,7 @@ private void regexComparison() { } private void bitsComparison() { - // Queries for documents that have a "bitField" field with bits set at the bitmask "34" positions + // Prints all documents that contain values in a field called "a" that match the bitmask value of "34" // begin bitsComparison Bson bitsComparison = bitsAllSet("a", 34); collection.find(bitsComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -133,21 +131,19 @@ private void bitsComparison() { private void geoWithinComparison() { // begin geoWithinComparison - // Creates a Polygon to filter the results Polygon square = new Polygon(Arrays.asList(new Position(0, 0), new Position(4, 0), new Position(4, 4), new Position(0, 4), new Position(0, 0))); - // Matches documents with "coordinates" values that fall within the Polygon filter + // Prints documents that contain "coordinates" values that are within the bounds of the polygon passed as the filter parameter Bson geoWithinComparison = geoWithin("coordinates", square); collection.find(geoWithinComparison).forEach(doc -> System.out.println(doc.toJson())); // end geoWithinComparison } private void preview(){ - // Uses an empty filter to print all documents in the collection Bson filter = new Document(); List res = new ArrayList(); System.out.println(collection.find(filter).into(res)); @@ -158,7 +154,6 @@ private void setupPaintCollection() { List filterdata = new ArrayList<>(); - // Set up the "vendor" field for your documents String [] p1a = {"A"}; String [] p2a = {"C", "D"}; String [] p3a = {"B","A"}; @@ -168,7 +163,6 @@ private void setupPaintCollection() { String [] p7a = {"B", "C"}; String [] p8a = {"A", "D"}; - // Creates a series of documents and inserts them into the "paint" collection Document p1 = new Document("_id", 1).append("color", "red").append("qty", 5).append("vendor", Arrays.asList(p1a)); Document p2 = new Document("_id", 2).append("color", "purple").append("qty", 10).append("vendor", Arrays.asList(p2a)); Document p3 = new Document("_id", 3).append("color", "blue").append("qty", 8).append("vendor", Arrays.asList(p3a)); @@ -194,7 +188,6 @@ private void setupPaintCollection() { private void setupBinaryCollection(){ List filterdata = new ArrayList<>(); - // Creates a series of documents and inserts them into the "binary" collection Document p9 = new Document("_id", 9).append("a", 54).append("binaryValue", "00110110"); Document p10 = new Document("_id", 10).append("a", 20).append("binaryValue", "00010100"); Document p11 = new Document("_id", 11).append("a", 68).append("binaryValue", "1000100"); @@ -211,7 +204,6 @@ private void setupPointsCollection(){ List filterdata = new ArrayList<>(); - // Creates a series of documents and inserts them into the "points" collection Document p13 = new Document("_id", 13).append("coordinates", new Point(new Position(2, 2))); Document p14 = new Document("_id", 14).append("coordinates", new Point(new Position(5, 6))); Document p15 = new Document("_id", 15).append("coordinates", new Point(new Position(1, 3))); diff --git a/source/includes/fundamentals/code-snippets/Geo.java b/source/includes/fundamentals/code-snippets/Geo.java index cccbc68ce..675c9042f 100644 --- a/source/includes/fundamentals/code-snippets/Geo.java +++ b/source/includes/fundamentals/code-snippets/Geo.java @@ -35,7 +35,6 @@ public static void main(String[] args) { } public void go() { - // Creates a Geo instance and runs geospatial queries on your collection Geo geo = new Geo(); System.out.println("Near Example: "); geo.nearExample(); @@ -47,21 +46,19 @@ public void go() { private void nearExample() { // begin findExample - // code to set up your mongo client ... - // Accesses the "theaters" collection in the "sample_mflix" database + // Add your MongoCollection setup code here MongoDatabase database = mongoClient.getDatabase("sample_mflix"); MongoCollection collection = database.getCollection("theaters"); - // Stores the coordinates of Central Park's Great Lawn in a Point instance Point centralPark = new Point(new Position(-73.9667, 40.78)); - // Queries for theaters between 5,000 and 10,000 meters from the specified Point + // Builds a query that matches all theaters between 5,000 and 10,000 meters from the specified Point Bson query = near("location.geo", centralPark, 10000.0, 5000.0); - // Specifies a projection to project the documents' "location.address.city" field + // Builds a projection to include only the "location.address.city" field in the results Bson projection = fields(include("location.address.city"), excludeId()); - // Runs the find operation with the specified query filter and projection + // Prints the projected field of the results from the geolocation query collection.find(query) .projection(projection) .forEach(doc -> System.out.println(doc.toJson())); @@ -69,25 +66,24 @@ private void nearExample() { } private void rangeExample() { - // Accesses the "theaters" collection in the "sample_mflix" database MongoDatabase database = mongoClient.getDatabase("sample_mflix"); MongoCollection collection = database.getCollection("theaters"); // begin rangeExample - // code to set up your mongo collection ... - // Creates a Polygon instance to filter for locations within a section of Long Island + // Add your MongoCollection setup code here + // Creates a Polygon instance for a geolocation query Polygon longIslandTriangle = new Polygon(Arrays.asList(new Position(-72, 40), new Position(-74, 41), new Position(-72, 39), new Position(-72, 40))); - // Specifies a projection to project the documents' "location.address.city" field + // Builds a projection to include only the "location.address.city" field in the results Bson projection = fields(include("location.address.city"), excludeId()); // Constructs a query with the $geoWithin query operator Bson geoWithinComparison = geoWithin("location.geo", longIslandTriangle); - // Runs the find operation with the specified query filter and projection + // Prints the projected field of the results from the geolocation query collection.find(geoWithinComparison) .projection(projection) .forEach(doc -> System.out.println(doc.toJson())); From ee6d22571209c6e2525950b811a57467126282b9 Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 18 Oct 2023 18:08:53 -0400 Subject: [PATCH 4/5] sheet syntax --- .../fundamentals/code-snippets/CurrentAPI.java | 2 +- .../fundamentals/code-snippets/Cursor.java | 8 ++++---- .../fundamentals/code-snippets/Delete.java | 6 +++--- .../fundamentals/code-snippets/Filters.java | 14 +++++++------- .../includes/fundamentals/code-snippets/Geo.java | 12 ++++++------ 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/source/includes/fundamentals/code-snippets/CurrentAPI.java b/source/includes/fundamentals/code-snippets/CurrentAPI.java index f14ba8ac8..991e5edf8 100644 --- a/source/includes/fundamentals/code-snippets/CurrentAPI.java +++ b/source/includes/fundamentals/code-snippets/CurrentAPI.java @@ -32,7 +32,7 @@ private void example1() { MongoDatabase db = client.getDatabase(DATABASE); MongoCollection col = db.getCollection(COLLECTION); - // Prints the first document retrieved from the collection in JSON format + // Prints the first document retrieved from the collection as JSON Document doc = col.find().first(); System.out.println(doc.toJson()); // end current-api-example diff --git a/source/includes/fundamentals/code-snippets/Cursor.java b/source/includes/fundamentals/code-snippets/Cursor.java index 57f9df612..5cf2350a4 100644 --- a/source/includes/fundamentals/code-snippets/Cursor.java +++ b/source/includes/fundamentals/code-snippets/Cursor.java @@ -59,7 +59,7 @@ public static void main(String [] args){ } private void forEachIteration(){ - // Prints all the document in the collection in JSON format + // Prints the JSON representation of all documents in the collection // begin forEachIteration FindIterable iterable = collection.find(); iterable.forEach(doc -> System.out.println(doc.toJson())); @@ -75,7 +75,7 @@ private void firstExample(){ } private void availableExample(){ - // Prints the number of query results that available in the returned cursor + // Prints the number of query results that are available in the returned cursor // begin availableExample MongoCursor cursor = collection.find().cursor(); System.out.println(cursor.available()); @@ -92,7 +92,7 @@ private void explainExample(){ } private void intoExample(){ - // Prints the query results + // Prints the results of the find operation as a list // begin intoExample List results = new ArrayList<>(); FindIterable iterable = collection.find(); @@ -102,7 +102,7 @@ private void intoExample(){ } private void manualIteration(){ - // Iterates through cursor results of a query and prints them as JSON + // Prints the results of the find operation by iterating through a cursor // begin manualIteration MongoCursor cursor = collection.find().cursor(); while (cursor.hasNext()){ diff --git a/source/includes/fundamentals/code-snippets/Delete.java b/source/includes/fundamentals/code-snippets/Delete.java index 208cafb5b..dc6d67eea 100644 --- a/source/includes/fundamentals/code-snippets/Delete.java +++ b/source/includes/fundamentals/code-snippets/Delete.java @@ -60,7 +60,7 @@ private void deleteManyExample(){ } private void findOneAndDeleteExample(){ - // Deletes the first document with a "color" value of "purple", and prints the deleted document + // Deletes the first document with a "color" value of "purple" and prints the deleted document // begin findOneAndDeleteExample Bson filter = Filters.eq("color", purple); System.out.println(collection.findOneAndDelete(filter).toJson()); @@ -68,7 +68,7 @@ private void findOneAndDeleteExample(){ } private void findOneAndDeleteNullExample(){ - // Deletes the first document with a "qty" value of 1, and prints the deleted document + // Deletes the first document with a "qty" value of 1 and prints the deleted document // begin findOneAndDeleteNullExample Bson filter = Filters.eq("qty", 1); System.out.println(collection.findOneAndDelete(filter)); @@ -83,7 +83,7 @@ private void deleteOneExample(){ // end deleteOneExample } private void preview(boolean drop){ - // Prints each document in your collection + // Prints the results of a find operation as JSON Bson filter = Filters.empty(); collection.find(filter).forEach(doc -> System.out.println(doc.toJson())); diff --git a/source/includes/fundamentals/code-snippets/Filters.java b/source/includes/fundamentals/code-snippets/Filters.java index 96c7440a1..306a974ec 100644 --- a/source/includes/fundamentals/code-snippets/Filters.java +++ b/source/includes/fundamentals/code-snippets/Filters.java @@ -65,7 +65,7 @@ public static void main(String[] args) { } private void equalComparison() { - // Prints all documents in a collection that contain a "qty" value of "5" + // Prints all documents in a collection that have a "qty" value of "5" as JSON // begin equalComparison Bson equalComparison = eq("qty", 5); collection.find(equalComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -73,7 +73,7 @@ private void equalComparison() { } private void gteComparison() { - // Prints all documents in a collection with a "qty" field value greater than "10" + // Prints all documents in a collection that have a "qty" value greater than "10" as JSON // begin gteComparison Bson gteComparison = gte("qty", 10); collection.find(gteComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -81,7 +81,7 @@ private void gteComparison() { } private void orComparison() { - // Prints all documents in a collection with a "qty" value of "8" or a "color" value of "pink" + // Prints all documents in a collection that have a "qty" value of "8" or a "color" value of "pink" as JSON // begin orComparison Bson orComparison = or(gt("qty", 8), eq("color", "pink")); collection.find(orComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -89,7 +89,7 @@ private void orComparison() { } private void emptyComparison() { - // Prints all documents in a collection + // Prints all documents in a collection as JSON // begin emptyComparison Bson emptyComparison = empty(); collection.find(emptyComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -97,7 +97,7 @@ private void emptyComparison() { } private void allComparison() { - // Prints all documents in which the "vendor" field contains all the elements of a list + // Prints all documents in which the "vendor" field contains all the elements of a list as JSON // begin allComparison List search = Arrays.asList("A", "D"); Bson allComparison = all("vendor", search); @@ -106,7 +106,7 @@ private void allComparison() { } private void existsComparison() { - // Prints documents in which a "qty" field exists and the value is not "5" or "8" + // Prints documents in which a "qty" field exists and the value is not "5" or "8" as JSON // begin existsComparison Bson existsComparison = and(exists("qty"), nin("qty", 5, 8)); collection.find(existsComparison).forEach(doc -> System.out.println(doc.toJson())); @@ -114,7 +114,7 @@ private void existsComparison() { } private void regexComparison() { - // Prints all documents in which the "color" field value starts with "p" + // Prints all documents in which the "color" field value starts with "p" as JSON // begin regexComparison Bson regexComparison = regex("color", "^p"); collection.find(regexComparison).forEach(doc -> System.out.println(doc.toJson())); diff --git a/source/includes/fundamentals/code-snippets/Geo.java b/source/includes/fundamentals/code-snippets/Geo.java index 675c9042f..271f56dfc 100644 --- a/source/includes/fundamentals/code-snippets/Geo.java +++ b/source/includes/fundamentals/code-snippets/Geo.java @@ -52,13 +52,13 @@ private void nearExample() { Point centralPark = new Point(new Position(-73.9667, 40.78)); - // Builds a query that matches all theaters between 5,000 and 10,000 meters from the specified Point + // Creates a query that matches all theaters between 5,000 and 10,000 meters from the specified Point Bson query = near("location.geo", centralPark, 10000.0, 5000.0); - // Builds a projection to include only the "location.address.city" field in the results + // Creates a projection to include only the "location.address.city" field in the results Bson projection = fields(include("location.address.city"), excludeId()); - // Prints the projected field of the results from the geolocation query + // Prints the projected field of the results from the geolocation query as JSON collection.find(query) .projection(projection) .forEach(doc -> System.out.println(doc.toJson())); @@ -77,13 +77,13 @@ private void rangeExample() { new Position(-72, 39), new Position(-72, 40))); - // Builds a projection to include only the "location.address.city" field in the results + // Creates a projection to include only the "location.address.city" field in the results Bson projection = fields(include("location.address.city"), excludeId()); - // Constructs a query with the $geoWithin query operator + // Creates a query that matches documents containing "location.geo" values within the specified Polygon Bson geoWithinComparison = geoWithin("location.geo", longIslandTriangle); - // Prints the projected field of the results from the geolocation query + // Prints the projected field of the results from the geolocation query as JSON collection.find(geoWithinComparison) .projection(projection) .forEach(doc -> System.out.println(doc.toJson())); From cd22bf6f5ebba1a86bdb6ef61ca1ccd6faee3268 Mon Sep 17 00:00:00 2001 From: norareidy Date: Fri, 27 Oct 2023 13:26:23 -0400 Subject: [PATCH 5/5] final feedback --- .../fundamentals/code-snippets/CurrentAPI.java | 1 - .../includes/fundamentals/code-snippets/Delete.java | 4 ++-- source/includes/fundamentals/code-snippets/Geo.java | 12 ++++++------ 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/source/includes/fundamentals/code-snippets/CurrentAPI.java b/source/includes/fundamentals/code-snippets/CurrentAPI.java index 991e5edf8..6eaca1748 100644 --- a/source/includes/fundamentals/code-snippets/CurrentAPI.java +++ b/source/includes/fundamentals/code-snippets/CurrentAPI.java @@ -26,7 +26,6 @@ public static void main(String[] args) throws InterruptedException { } private void example1() { - // Connects to MongoDB and retrieves a document by using the non-legacy API // start current-api-example MongoClient client = MongoClients.create(URI); MongoDatabase db = client.getDatabase(DATABASE); diff --git a/source/includes/fundamentals/code-snippets/Delete.java b/source/includes/fundamentals/code-snippets/Delete.java index dc6d67eea..98d015097 100644 --- a/source/includes/fundamentals/code-snippets/Delete.java +++ b/source/includes/fundamentals/code-snippets/Delete.java @@ -62,7 +62,7 @@ private void deleteManyExample(){ private void findOneAndDeleteExample(){ // Deletes the first document with a "color" value of "purple" and prints the deleted document // begin findOneAndDeleteExample - Bson filter = Filters.eq("color", purple); + Bson filter = Filters.eq("color", "purple"); System.out.println(collection.findOneAndDelete(filter).toJson()); // end findOneAndDeleteExample } @@ -83,7 +83,7 @@ private void deleteOneExample(){ // end deleteOneExample } private void preview(boolean drop){ - // Prints the results of a find operation as JSON + // Prints all documents in a collection as JSON Bson filter = Filters.empty(); collection.find(filter).forEach(doc -> System.out.println(doc.toJson())); diff --git a/source/includes/fundamentals/code-snippets/Geo.java b/source/includes/fundamentals/code-snippets/Geo.java index 271f56dfc..8ac60c433 100644 --- a/source/includes/fundamentals/code-snippets/Geo.java +++ b/source/includes/fundamentals/code-snippets/Geo.java @@ -46,19 +46,19 @@ public void go() { private void nearExample() { // begin findExample - // Add your MongoCollection setup code here + // Add your MongoClient setup code here MongoDatabase database = mongoClient.getDatabase("sample_mflix"); MongoCollection collection = database.getCollection("theaters"); Point centralPark = new Point(new Position(-73.9667, 40.78)); - // Creates a query that matches all theaters between 5,000 and 10,000 meters from the specified Point + // Creates a query that matches all locations between 5,000 and 10,000 meters from the specified Point Bson query = near("location.geo", centralPark, 10000.0, 5000.0); // Creates a projection to include only the "location.address.city" field in the results Bson projection = fields(include("location.address.city"), excludeId()); - // Prints the projected field of the results from the geolocation query as JSON + // Prints the projected field of the results from the geospatial query as JSON collection.find(query) .projection(projection) .forEach(doc -> System.out.println(doc.toJson())); @@ -69,9 +69,9 @@ private void rangeExample() { MongoDatabase database = mongoClient.getDatabase("sample_mflix"); MongoCollection collection = database.getCollection("theaters"); // begin rangeExample - // Add your MongoCollection setup code here - // Creates a Polygon instance for a geolocation query + + // Creates a set of points that defines the bounds of a geospatial shape Polygon longIslandTriangle = new Polygon(Arrays.asList(new Position(-72, 40), new Position(-74, 41), new Position(-72, 39), @@ -80,7 +80,7 @@ private void rangeExample() { // Creates a projection to include only the "location.address.city" field in the results Bson projection = fields(include("location.address.city"), excludeId()); - // Creates a query that matches documents containing "location.geo" values within the specified Polygon + // Creates a query that matches documents containing "location.geo" values within the specified bounds Bson geoWithinComparison = geoWithin("location.geo", longIslandTriangle); // Prints the projected field of the results from the geolocation query as JSON