Skip to content

Commit 827b3b3

Browse files
committed
MK feedback
1 parent fbd07f6 commit 827b3b3

File tree

5 files changed

+39
-58
lines changed

5 files changed

+39
-58
lines changed

source/crud/read-operations/retrieve.txt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ Use the find operation to retrieve your documents from MongoDB. You can specify
5656
which documents to retrieve, in what order to retrieve them, and how many to
5757
retrieve.
5858

59-
To perform a find operation, call the ``find()`` method on an instance
60-
of a ``MongoCollection``. This method searches a collection for documents that
61-
match the query filter you provide. For more information about how to
62-
specify a query, see our :doc:`Specify a Query
63-
</crud/query-document>` guide.
59+
Call the ``find()`` method on an instance of a ``MongoCollection`` to filter for
60+
documents that match the provided query. For more information about how to
61+
specify a query, see our :doc:`Specify a Query </crud/query-document>` guide.
62+
You can then use methods such as ``forEach()`` or ``cursor()`` to retrieve
63+
matching documents. For more information, see the `FindIterable
64+
<{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/FindIterable.html>`__
65+
API documentation.
6466

6567
To retrieve a single document, you can add the ``first()`` method to your
6668
``find()`` call. To choose a specific document, you can use the ``sort()``
@@ -106,8 +108,11 @@ Find Example: Full File
106108

107109
This example is a complete, standalone file that performs the following actions:
108110

109-
- Calls the ``find()`` method to retrieve multiple documents that have a ``runtime`` value less than ``15``, applying a projection and sort to the results
110-
- Calls the ``find()`` and ``first()`` methods to retrieve a document that has a ``title`` value of ``"The Room"``, applying a projection and sort before returning the first match
111+
- Calls the ``find()`` method to retrieve 10 documents that has a ``runtime``
112+
value less than ``15`` minutes, applying a projection and sort to the results
113+
- Calls the ``find()`` and ``first()`` methods to retrieve the document with the
114+
highest ``imdb.rating`` that is has a ``runtime`` value less than ``15``
115+
minutes, applying a projection to the result
111116

112117
.. io-code-block::
113118

source/crud/write-operations/delete.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ operation and a delete many operation:
176176
:language: none
177177
:visible: false
178178

179-
deleteOne() document count: 1
180-
deleteMany() document count: 4
179+
Deleted document count - query for one: 1
180+
Deleted document count - unlimited query: 4
181181

182182

183183
The queries in these examples use the ``eq()`` and ``lt()`` filters to query documents. For more

source/includes/crud/Delete.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Deletes a document from a collection by using the Java driver
1+
// Deletes documents from a collection by using the Java driver
22

33
package org.example;
44

@@ -22,36 +22,24 @@ public static void main(String[] args) {
2222
// Replace the uri string with your MongoDB deployment's connection string
2323
String uri = "<connection string uri>";
2424

25-
try (MongoClient mongoClient = MongoClients.create(uri)) {
25+
try (MongoClient mongoClient = MongoClients.create(uri)) {
2626

2727
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
2828
MongoCollection<Document> collection = database.getCollection("movies");
2929

3030
Bson deleteOneQuery = eq("title", "The Garbage Pail Kids Movie");
3131

32-
try {
33-
// Deletes the first document that has a "title" value of "The Garbage Pail Kids Movie"
34-
DeleteResult result = collection.deleteOne(deleteOneQuery);
35-
System.out.println("deleteOne() document count: " + result.getDeletedCount());
36-
37-
// Prints a message if any exceptions occur during the operation
38-
} catch (MongoException me) {
39-
System.err.println("Unable to delete due to an error: " + me);
40-
}
32+
// Deletes the first document that has a "title" value of "The Garbage Pail Kids Movie"
33+
DeleteResult result = collection.deleteOne(deleteOneQuery);
34+
System.out.println("Deleted document count - query for 1: " + result.getDeletedCount());
4135

4236
Bson deleteManyQuery = lt("imdb.rating", 1.9);
4337

44-
try {
45-
// Deletes all documents that have an "imdb.rating" value less than 1.9
46-
DeleteResult result = collection.deleteMany(deleteManyQuery);
47-
48-
// Prints the number of deleted documents
49-
System.out.println("deleteMany() document count: " + result.getDeletedCount());
50-
51-
// Prints a message if any exceptions occur during the operation
52-
} catch (MongoException me) {
53-
System.err.println("Unable to delete due to an error: " + me);
54-
}
38+
// Deletes all documents that have an "imdb.rating" value less than 1.9
39+
result = collection.deleteMany(deleteManyQuery);
40+
41+
// Prints the number of deleted documents
42+
System.out.println("Deleted document count - unlimited query: " + result.getDeletedCount());
5543
}
5644
}
5745
}

source/includes/crud/Find.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ public static void main( String[] args ) {
3939
.sort(Sorts.ascending("title"))
4040
.limit(10);
4141

42-
// Prints the results of the find operation as JSON
43-
System.out.print("10 movies under 15 minutes: ");
44-
docs.forEach(doc -> System.out.print(doc.get("title") + ", "));
42+
// Prints the titles of the queried documents
43+
System.out.println("10 movies under 15 minutes: ");
44+
docs.forEach(doc -> System.out.println("- " + doc.get("title")));
45+
System.out.println();
4546

4647
// Retrieves the document with the best imdb rating that is less
4748
// than 15 minutes long, applying the projection
@@ -50,11 +51,11 @@ public static void main( String[] args ) {
5051
.sort(Sorts.ascending("imdb.rating"))
5152
.first();
5253

53-
// Prints result document as JSON
54+
// Prints title of the queried document
5455
if (doc == null) {
5556
System.out.println("No results found.");
5657
} else {
57-
System.out.println("\n\nThe highest rated movie under 15 minutes: " + doc.toJson());
58+
System.out.println("The highest rated movie under 15 minutes: " + doc.toJson().get("title"));
5859
}
5960
}
6061
}

source/includes/crud/Update.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public static void main(String[] args) {
2929
// Instructs the driver to insert a new document if none match the query
3030
UpdateOptions options = new UpdateOptions().upsert(true);
3131

32-
3332
Document updateOneQuery = new Document().append("title", "Cool Runnings 2");
3433

3534
// Creates instructions to update the values of three document fields
@@ -38,18 +37,12 @@ public static void main(String[] args) {
3837
Updates.addToSet("genres", "Sports"),
3938
Updates.currentTimestamp("lastUpdated"));
4039

41-
try {
42-
// Updates the first document that has a "title" value of "Cool Runnings 2"
43-
UpdateResult result = collection.updateOne(updateOneQuery, updateOneUpdates, options);
40+
// Updates the first document that has a "title" value of "Cool Runnings 2"
41+
UpdateResult result = collection.updateOne(updateOneQuery, updateOneUpdates, options);
4442

45-
// Prints the number of updated documents and the upserted document ID, if an upsert was performed
46-
System.out.println("updateOne() modified document count: " + result.getModifiedCount());
47-
System.out.println("Upserted ID: " + result.getUpsertedId());
48-
49-
// Prints a message if any exceptions occur during the operation
50-
} catch (MongoException me) {
51-
System.err.println("Unable to update due to an error: " + me);
52-
}
43+
// Prints the number of updated documents and the upserted document ID, if an upsert was performed
44+
System.out.println("updateOne() modified document count: " + result.getModifiedCount());
45+
System.out.println("Upserted ID: " + result.getUpsertedId());
5346

5447
Bson updateManyQuery = gt("num_mflix_comments", 50);
5548

@@ -58,17 +51,11 @@ public static void main(String[] args) {
5851
Updates.addToSet("genres", "Frequently Discussed"),
5952
Updates.currentTimestamp("lastUpdated"));
6053

61-
try {
62-
// Updates documents that have a "num_mflix_comments" value over 50
63-
UpdateResult result = collection.updateMany(updateManyQuery, updateManyUpdates);
64-
65-
// Prints the number of updated documents
66-
System.out.println("\nupdateMany() modified document count: " + result.getModifiedCount());
54+
// Updates documents that have a "num_mflix_comments" value over 50
55+
UpdateResult result = collection.updateMany(updateManyQuery, updateManyUpdates);
6756

68-
// Prints a message if any exceptions occur during the operation
69-
} catch (MongoException me) {
70-
System.err.println("Unable to update due to an error: " + me);
71-
}
57+
// Prints the number of updated documents
58+
System.out.println("\nupdateMany() modified document count: " + result.getModifiedCount());
7259
}
7360
}
7461
}

0 commit comments

Comments
 (0)