Skip to content

DOCSP-33345: Java comments pt. 5 #473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions source/includes/fundamentals/code-snippets/CurrentAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class CurrentAPI {

public static void main(String[] args) throws InterruptedException {
CurrentAPI c = new CurrentAPI();

c.example1();
c.example2();

Expand All @@ -29,13 +30,16 @@ private void example1() {
MongoClient client = MongoClients.create(URI);
MongoDatabase db = client.getDatabase(DATABASE);
MongoCollection<Document> col = db.getCollection(COLLECTION);

// Prints the first document retrieved from the collection as JSON
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue:
I think it could be good to only feature one Summary-type comment per method.

Suggestion:
I think omitting the Summary-type comment on line 29 ("Connects to MongoDB and retrieves a document...") would work well.

Document doc = col.find().first();
System.out.println(doc.toJson());
// end current-api-example
client.close();
}

private void example2() {
// 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))
Expand Down
9 changes: 9 additions & 0 deletions source/includes/fundamentals/code-snippets/Cursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,31 @@ public static void main(String [] args){
}

private void forEachIteration(){
// Prints the JSON representation of all documents in the collection
// begin forEachIteration
FindIterable<Document> iterable = collection.find();
iterable.forEach(doc -> System.out.println(doc.toJson()));
// end forEachIteration
}

private void firstExample(){
// Prints the first document that matches the query
// begin firstExample
FindIterable<Document> iterable = collection.find();
System.out.println(iterable.first());
// end firstExample
}

private void availableExample(){
// Prints the number of query results that are available in the returned cursor
// begin availableExample
MongoCursor<Document> cursor = collection.find().cursor();
System.out.println(cursor.available());
// end availableExample
}

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

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

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

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

Expand All @@ -120,6 +127,7 @@ private void closeExample(){
}

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

public void setupPaintCollection(){
collection.drop();

collection.insertMany(Arrays.asList(
new Document("_id", 1).append("color", "red").append("qty", 5),
new Document("_id", 2).append("color", "purple").append("qty", 10),
Expand Down
8 changes: 7 additions & 1 deletion source/includes/fundamentals/code-snippets/Delete.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,35 +52,41 @@ public static void main(String [] args){
}

private void deleteManyExample(){
// Deletes all documents in the collection that contain a "qty" value of 0
// begin deleteManyExample
Bson filter = Filters.eq("qty", 0);
collection.deleteMany(filter);
// end 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
}

private void findOneAndDeleteNullExample(){
// 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));
// end findOneAndDeleteNullExample
}

private void deleteOneExample(){
// Deletes the first 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){
// Prints all documents in a collection as JSON
Bson filter = Filters.empty();
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));

if (drop){
collection.drop();
}
Expand Down
10 changes: 10 additions & 0 deletions source/includes/fundamentals/code-snippets/Filters.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,34 +65,39 @@ public static void main(String[] args) {
}

private void equalComparison() {
// 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()));
// end equalComparison
}

private void gteComparison() {
// 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()));
// end gteComparison
}

private void orComparison() {
// 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()));
// end orComparison
}

private void emptyComparison() {
// Prints all documents in a collection as JSON
// begin emptyComparison
Bson emptyComparison = empty();
collection.find(emptyComparison).forEach(doc -> System.out.println(doc.toJson()));
// end emptyComparison
}

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

private void existsComparison() {
// 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()));
// end existsComparison
}

private void regexComparison() {
// 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()));
// end regexComparison
}

private void bitsComparison() {
// 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()));
Expand All @@ -129,6 +137,7 @@ private void geoWithinComparison() {
new Position(0, 4),
new Position(0, 0)));

// 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
Expand All @@ -144,6 +153,7 @@ private void preview(){
private void setupPaintCollection() {

List<Document> filterdata = new ArrayList<>();

String [] p1a = {"A"};
String [] p2a = {"C", "D"};
String [] p3a = {"B","A"};
Expand Down
18 changes: 16 additions & 2 deletions source/includes/fundamentals/code-snippets/Geo.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ public void go() {
private void nearExample() {
// begin findExample

// code to set up your mongo client ...
// Add your MongoClient setup code here
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection<Document> collection = database.getCollection("theaters");

Point centralPark = new Point(new Position(-73.9667, 40.78));

// 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 geospatial query as JSON
collection.find(query)
.projection(projection)
.forEach(doc -> System.out.println(doc.toJson()));
Expand All @@ -62,14 +69,21 @@ private void rangeExample() {
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
MongoCollection<Document> collection = database.getCollection("theaters");
// begin rangeExample
// Add your MongoCollection setup code here
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
I think it would be good to include a linebreak between two separate comments so it can be read and understood as a separate comment. I think the blank line after the Marker comment ("begin rangeExample") is not necessary.


// code to set up your mongo collection ...
// 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),
new Position(-72, 40)));

// 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 bounds
Bson geoWithinComparison = geoWithin("location.geo", longIslandTriangle);

// 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()));
Expand Down