-
Notifications
You must be signed in to change notification settings - Fork 43
DOCSP-33345: Java code comments pt. 2 #470
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,3 +1,11 @@ | ||||||||||||||||||
/** | ||||||||||||||||||
* This file demonstrates how to find multiple documents by using the Java driver. | ||||||||||||||||||
* It connects to a MongoDB deployment, accesses the "sample_mflix" database, and finds | ||||||||||||||||||
* documents in the "movies" collection that match the specified query filter. | ||||||||||||||||||
* The code projects certain fields in the returned documents and sorts them according to | ||||||||||||||||||
* specified criteria. | ||||||||||||||||||
*/ | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
package usage.examples; | ||||||||||||||||||
|
||||||||||||||||||
import static com.mongodb.client.model.Filters.lt; | ||||||||||||||||||
|
@@ -20,18 +28,20 @@ public static void main( String[] args ) { | |||||||||||||||||
String uri = "<connection string uri>"; | ||||||||||||||||||
|
||||||||||||||||||
try (MongoClient mongoClient = MongoClients.create(uri)) { | ||||||||||||||||||
|
||||||||||||||||||
MongoDatabase database = mongoClient.getDatabase("sample_mflix"); | ||||||||||||||||||
MongoCollection<Document> collection = database.getCollection("movies"); | ||||||||||||||||||
|
||||||||||||||||||
// Creates instructions to project two document fields | ||||||||||||||||||
Bson projectionFields = Projections.fields( | ||||||||||||||||||
Projections.include("title", "imdb"), | ||||||||||||||||||
Projections.excludeId()); | ||||||||||||||||||
|
||||||||||||||||||
// Runs a find operation that matches documents with a "runtime" value under 15 | ||||||||||||||||||
MongoCursor<Document> cursor = collection.find(lt("runtime", 15)) | ||||||||||||||||||
.projection(projectionFields) | ||||||||||||||||||
.sort(Sorts.descending("title")).iterator(); | ||||||||||||||||||
|
||||||||||||||||||
// Prints two fields of each matching document and sorts the result documents | ||||||||||||||||||
try { | ||||||||||||||||||
while(cursor.hasNext()) { | ||||||||||||||||||
System.out.println(cursor.next().toJson()); | ||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,3 +1,10 @@ | ||||||||||||||||
/** | ||||||||||||||||
* This file demonstrates how to find a document by using the Java driver. | ||||||||||||||||
* It connects to a MongoDB deployment, accesses the "sample_mflix" database, and finds | ||||||||||||||||
* a document in the "movies" collection that matches the specified query filter. | ||||||||||||||||
* The code projects certain fields in the returned document. | ||||||||||||||||
*/ | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
package usage.examples; | ||||||||||||||||
|
||||||||||||||||
import static com.mongodb.client.model.Filters.eq; | ||||||||||||||||
|
@@ -20,19 +27,21 @@ public static void main( String[] args ) { | |||||||||||||||
String uri = "<connection string uri>"; | ||||||||||||||||
|
||||||||||||||||
try (MongoClient mongoClient = MongoClients.create(uri)) { | ||||||||||||||||
|
||||||||||||||||
MongoDatabase database = mongoClient.getDatabase("sample_mflix"); | ||||||||||||||||
MongoCollection<Document> collection = database.getCollection("movies"); | ||||||||||||||||
|
||||||||||||||||
// Creates instructions to project two document fields | ||||||||||||||||
Bson projectionFields = Projections.fields( | ||||||||||||||||
Projections.include("title", "imdb"), | ||||||||||||||||
Projections.excludeId()); | ||||||||||||||||
|
||||||||||||||||
// Runs a find operation that retrieves only the first matching document | ||||||||||||||||
Document doc = collection.find(eq("title", "The Room")) | ||||||||||||||||
.projection(projectionFields) | ||||||||||||||||
.sort(Sorts.descending("imdb.rating")) | ||||||||||||||||
.first(); | ||||||||||||||||
|
||||||||||||||||
// Prints a message if there are no result documents, or prints two fields of the matched document | ||||||||||||||||
if (doc == null) { | ||||||||||||||||
System.out.println("No results found."); | ||||||||||||||||
} else { | ||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,3 +1,10 @@ | ||||||||||||||||
/** | ||||||||||||||||
* This file demonstrates how to insert multiple documents into a collection by | ||||||||||||||||
* using the Java driver. | ||||||||||||||||
* The file connects to a MongoDB deployment, accesses the "sample_mflix" database, | ||||||||||||||||
* and inserts two new documents into the "movies" collection. | ||||||||||||||||
*/ | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
package usage.examples; | ||||||||||||||||
|
||||||||||||||||
import java.util.Arrays; | ||||||||||||||||
|
@@ -19,18 +26,22 @@ public static void main(String[] args) { | |||||||||||||||
String uri = "<connection string uri>"; | ||||||||||||||||
|
||||||||||||||||
try (MongoClient mongoClient = MongoClients.create(uri)) { | ||||||||||||||||
|
||||||||||||||||
MongoDatabase database = mongoClient.getDatabase("sample_mflix"); | ||||||||||||||||
MongoCollection<Document> collection = database.getCollection("movies"); | ||||||||||||||||
|
||||||||||||||||
// Creates two sample documents containing a "title" field | ||||||||||||||||
List<Document> movieList = Arrays.asList( | ||||||||||||||||
new Document().append("title", "Short Circuit 3"), | ||||||||||||||||
new Document().append("title", "The Lego Frozen Movie")); | ||||||||||||||||
|
||||||||||||||||
try { | ||||||||||||||||
// Runs a write operation that inserts sample documents into the collection | ||||||||||||||||
InsertManyResult result = collection.insertMany(movieList); | ||||||||||||||||
|
||||||||||||||||
// Prints the IDs of the inserted documents | ||||||||||||||||
System.out.println("Inserted document ids: " + result.getInsertedIds()); | ||||||||||||||||
|
||||||||||||||||
// Prints a message if the operation generates an error | ||||||||||||||||
} catch (MongoException me) { | ||||||||||||||||
System.err.println("Unable to insert due to an error: " + me); | ||||||||||||||||
} | ||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,3 +1,10 @@ | ||||||||||||||||
/** | ||||||||||||||||
* This file demonstrates how to insert a document into a collection by using the | ||||||||||||||||
* Java driver. | ||||||||||||||||
* The file connects to a MongoDB deployment, accesses the "sample_mflix" database, | ||||||||||||||||
* and inserts a new document into the "movies" collection. | ||||||||||||||||
*/ | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
package usage.examples; | ||||||||||||||||
|
||||||||||||||||
import java.util.Arrays; | ||||||||||||||||
|
@@ -18,17 +25,20 @@ public static void main(String[] args) { | |||||||||||||||
String uri = "<connection string uri>"; | ||||||||||||||||
|
||||||||||||||||
try (MongoClient mongoClient = MongoClients.create(uri)) { | ||||||||||||||||
|
||||||||||||||||
MongoDatabase database = mongoClient.getDatabase("sample_mflix"); | ||||||||||||||||
MongoCollection<Document> collection = database.getCollection("movies"); | ||||||||||||||||
|
||||||||||||||||
try { | ||||||||||||||||
// Runs a write operation that inserts a sample document into the collection | ||||||||||||||||
InsertOneResult result = collection.insertOne(new Document() | ||||||||||||||||
.append("_id", new ObjectId()) | ||||||||||||||||
.append("title", "Ski Bloopers") | ||||||||||||||||
.append("genres", Arrays.asList("Documentary", "Comedy"))); | ||||||||||||||||
|
||||||||||||||||
// Prints the ID of the inserted document | ||||||||||||||||
System.out.println("Success! Inserted document id: " + result.getInsertedId()); | ||||||||||||||||
|
||||||||||||||||
// Prints a message if the operation generates an error | ||||||||||||||||
} catch (MongoException me) { | ||||||||||||||||
System.err.println("Unable to insert due to an error: " + me); | ||||||||||||||||
} | ||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.