Skip to content

Commit 5b02415

Browse files
committed
MK feedback
1 parent bcdd1f8 commit 5b02415

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

source/crud/read-operations/retrieve.txt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,22 @@ track of the color and quantity, which corresponds to the ``color`` and
5252
Find Operation
5353
--------------
5454

55-
Use the find operation to retrieve a subset of your existing data in
56-
MongoDB. You can specify what data to return including which documents
57-
to retrieve, in what order to retrieve them, and how many to retrieve.
55+
Use the find operation to retrieve your documents from MongoDB. You can specify
56+
which documents to retrieve, in what order to retrieve them, and how many to
57+
retrieve.
5858

5959
To perform a find operation, call the ``find()`` method on an instance
6060
of a ``MongoCollection``. This method searches a collection for documents that
6161
match the query filter you provide. For more information about how to
6262
specify a query, see our :ref:`Specify a Query
6363
<java-query>` guide.
6464

65-
To retrieve a single document, you can append the ``first()`` method to your
66-
``find()`` operation. You can use the ``sort()`` operation before selecting the first
67-
document to help choose the correct file.
65+
To retrieve a single document, you can add the ``first()`` method to your
66+
``find()`` call. To choose a specific document, you can use the ``sort()``
67+
operation before selecting the first document. You may also want to use the
68+
``limit()`` method to optimize memory usage. For more information, see the
69+
server manual for more information about :manual:`memory optimization when using
70+
the sort operation </reference/operator/aggregation/sort/#-sort----limit-memory-optimization>`.
6871

6972
Example
7073
~~~~~~~
@@ -116,9 +119,9 @@ This example is a complete, standalone file that performs the following actions:
116119
:language: none
117120
:visible: false
118121

119-
Number of documents found with find(): 101
122+
10 movies under 15 minutes: 10 Minutes, 3x3, 7:35 in the Morning, 8, 9, A Chairy Tale, A Corner in Wheat, A Gentle Spirit, A Is for Autism, A Movie,
120123

121-
Document found with find().first(): {"title": "The Room", "imdb": {"rating": 3.5, "votes": 25673, "id": 368226}}
124+
The highest rated movie under 15 minutes: {"title": "Andrè and Wally B.", "imdb": {"rating": 5.4, "votes": 3294, "id": 86855}}
122125

123126
.. _retrieve-aggregate:
124127

@@ -181,6 +184,7 @@ on this page, see the following API documentation:
181184

182185
- `find() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#find()>`__
183186
- `first() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoIterable.html#first()>`__
187+
- `limit() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/FindIterable.html#limit(int)>`__
184188
- `FindIterable <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/FindIterable.html>`__
185189
- `aggregate() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#aggregate(java.util.List)>`__
186190

@@ -190,4 +194,6 @@ Server Manual Entries
190194
- :manual:`Collections </core/databases-and-collections/#collections>`
191195
- :manual:`Query Documents </tutorial/query-documents>`
192196
- :manual:`Aggregation </aggregation>`
197+
- :manual:`$sort </aggregation/sort>`
198+
- :manual:`$limit </aggregation/limit>`
193199
- :manual:`Aggregation stages </meta/aggregation-quick-reference/#stages>`

source/includes/crud/Find.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,34 @@ public static void main( String[] args ) {
2727
MongoDatabase database = mongoClient.getDatabase("sample_mflix");
2828
MongoCollection<Document> collection = database.getCollection("movies");
2929

30-
// Creates instructions to project two document fields
30+
// Projects "title" and "imdb" fields, excludes "_id"
3131
Bson projectionFields = Projections.fields(
3232
Projections.include("title", "imdb"),
3333
Projections.excludeId());
3434

35-
// Retrieves documents that match the filter, applying a projection and a descending sort to the results
36-
MongoCursor<Document> cursor = collection.find(lt("runtime", 15))
35+
// Retrieves documents with a runtime of less than 15 minutes, applying the
36+
// projection and a sorting in alphabetical order
37+
FindIterable<Document> docs = collection.find(lt("runtime", 15))
3738
.projection(projectionFields)
38-
.sort(Sorts.descending("title")).iterator();
39+
.sort(Sorts.ascending("title"))
40+
.limit(10);
3941

4042
// Prints the results of the find operation as JSON
41-
System.out.println("Number of documents found with find(): " + cursor.available() + "\n");
42-
cursor.close();
43+
System.out.print("10 movies under 15 minutes: ");
44+
docs.forEach(doc -> System.out.print(doc.get("title") + ", "));
4345

44-
// Retrieves the first matching document, applying a projection and a descending sort to the results
45-
Document doc = collection.find(eq("title", "The Room"))
46+
// Retrieves the document with the best imdb rating that is less
47+
// than 15 minutes long, applying the projection
48+
Document doc = collection.find(lt("runtime", 15))
4649
.projection(projectionFields)
47-
.sort(Sorts.descending("imdb.rating"))
50+
.sort(Sorts.ascending("imdb.rating"))
4851
.first();
4952

50-
// Prints a message if there are no result documents, or prints the result document as JSON
53+
// Prints result document as JSON
5154
if (doc == null) {
5255
System.out.println("No results found.");
5356
} else {
54-
System.out.println("Document found with find().first(): " + doc.toJson());
57+
System.out.println("\n\nThe highest rated movie under 15 minutes: " + doc.toJson());
5558
}
5659
}
5760
}

0 commit comments

Comments
 (0)