Skip to content

DOCSP-33345: Java comments pt. 9 #478

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 3 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
9 changes: 9 additions & 0 deletions source/includes/fundamentals/code-snippets/Retrieve.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,21 @@ public static void main(String [] args){
}

private void findExample(){
// Creates a filter to match documents that have a "qty" value between 3 and 9
// begin findExample
Bson filter = Filters.and(Filters.gt("qty", 3), Filters.lt("qty", 9));

// Retrieves documents that match the filter and prints them as JSON
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
// end findExample
}

private void aggregateExample(){
// Creates an empty filter to match all documents in the collection
// begin aggregateExample
Bson filter = Filters.empty();

// Prints the collection's "color" values and each value's frequency in descending frequency order
collection.aggregate(Arrays.asList(
Aggregates.match(filter),
Aggregates.group("$color", Accumulators.sum("qty", "$qty")),
Expand All @@ -69,13 +75,16 @@ private void aggregateExample(){
}

private void watchExample(){
// Creates instructions to match insert and update operations
// begin watchExample
List<Bson> pipeline = Arrays.asList(
Aggregates.match(Filters.in("operationType", Arrays.asList("insert", "update"))));

// Creates a change stream that receives change events for the specified operations
ChangeStreamIterable<Document> changeStream = database.watch(pipeline)
.fullDocument(FullDocument.UPDATE_LOOKUP);

// Prints a message each time the change stream receives a change event
changeStream.forEach(event ->
System.out.println("Received a change to the collection: " + event));
// end watchExample
Expand Down
4 changes: 4 additions & 0 deletions source/includes/fundamentals/code-snippets/SearchText.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,31 @@ public static void main(String [] args){
}

private void termExample(){
// Retrieves and prints documents containing the "fast" string in any fields associated with a text index
// begin termExample
Bson filter = Filters.text("fast");
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
// end termExample
}

private void multipleTermExample(){
// Retrieves and prints documents containing the "fate 7" string in any fields associated with a text index
// begin multipleTermExample
Bson filter = Filters.text("fate 7");
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
// end multipleTermExample
}

private void negateExample(){
// Retrieves and prints documents containing the "furious" but not the "fast" string in any fields associated with a text index
// begin negateExample
Bson filter = Filters.text("furious -fast");
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
// end negateExample
}

private void phraseExample(){
// Retrieves and prints documents containing the "fate of the furious" string in any fields associated with a text index
// begin phraseExample
Bson filter = Filters.text("\"fate of the furious\"");
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));
Expand Down
7 changes: 5 additions & 2 deletions source/includes/fundamentals/code-snippets/Skip.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ public static void main (String [] args){
}

private void skipExample(){
// Prints all remaining documents as JSON after applying a descending sort and omitting the first 5 documents
// begin skipExample
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Sorts;

// <MongoCollection setup code here>
// Add your MongoCollection setup code here

Bson filter = Filters.empty();
collection.find(filter)
Expand All @@ -54,6 +55,7 @@ private void skipExample(){
}

private void noResultsExample(){
// Prints all remaining documents as JSON after applying a descending sort and omitting the first 9 documents
// begin noResultsExample
Bson filter = Filters.empty();
collection.find(filter)
Expand All @@ -64,12 +66,13 @@ private void noResultsExample(){
}

private void skipAggregateExample(){
// Prints all remaining documents after using an aggregation pipeline to apply a sort and omit the first 5 documents
// begin skipAggregateExample
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Sorts;
import com.mongodb.client.model.Aggregates;

// <MongoCollection setup code here>
// Add your MongoCollection setup code here

Bson filter = Filters.empty();
collection.aggregate(Arrays.asList(
Expand Down