Skip to content

Commit e4999a6

Browse files
committed
MK feedback
1 parent 0c1dba8 commit e4999a6

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

source/crud/query-documents/find.txt

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

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

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

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

6770
Example
6871
~~~~~~~
@@ -114,9 +117,9 @@ This example is a complete, standalone file that performs the following actions:
114117
:language: none
115118
:visible: false
116119

117-
Number of documents found with find(): 101
120+
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,
118121

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

121124
.. _retrieve-aggregate:
122125

@@ -179,6 +182,7 @@ on this page, see the following API documentation:
179182

180183
- `find() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#find()>`__
181184
- `first() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoIterable.html#first()>`__
185+
- `limit() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/FindIterable.html#limit(int)>`__
182186
- `FindIterable <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/FindIterable.html>`__
183187
- `aggregate() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#aggregate(java.util.List)>`__
184188

@@ -188,4 +192,6 @@ Server Manual Entries
188192
- :manual:`Collections </core/databases-and-collections/#collections>`
189193
- :manual:`Query Documents </tutorial/query-documents>`
190194
- :manual:`Aggregation </aggregation>`
195+
- :manual:`$sort </aggregation/sort>`
196+
- :manual:`$limit </aggregation/limit>`
191197
- :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)