Skip to content

Commit 985d8ef

Browse files
committed
retrieve
1 parent e9dd4f3 commit 985d8ef

File tree

5 files changed

+69
-23
lines changed

5 files changed

+69
-23
lines changed

source/crud/read-operations/retrieve.txt

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
Retrieve Data
55
==============
66

7+
.. facet::
8+
:name: genre
9+
:values: reference
710

11+
.. meta::
12+
:keywords: find, findOne, findMany, get, lookup
13+
:description: Learn about how to retrieve data in the {+driver-long+}.
814

915
.. contents:: On this page
1016
:local:
@@ -84,8 +90,27 @@ The following shows the output of the preceding query:
8490
After the owner runs this query, they find two orders that matched the
8591
criteria.
8692

87-
For a runnable ``find()`` example, see our :ref:`Find Multiple
88-
Documents <retrieve-find>` page.
93+
Find Example: Full File
94+
~~~~~~~~~~~~~~~~~~~~~~~
95+
96+
The following code is a complete, standalone file that performs a find one
97+
operation and a find many operation.
98+
99+
.. include:: /includes/crud/example-intro.rst
100+
101+
.. io-code-block::
102+
103+
.. input:: /includes/crud/Find.java
104+
:language: java
105+
:dedent:
106+
107+
.. output::
108+
:language: none
109+
:visible: false
110+
111+
Number of documents found with find(): 101
112+
113+
Document found with find().first(){"title": "The Room", "imdb": {"rating": 3.5, "votes": 25673, "id": 368226}}
89114

90115
.. _retrieve-aggregate:
91116

@@ -101,7 +126,8 @@ instance of a ``MongoCollection``. This method accepts aggregation
101126
expressions to run in sequence. To perform aggregations, you can
102127
define aggregation stages that specify how to match documents, rename
103128
fields, and group values. For more information, see our
104-
:ref:`Aggregation <java-aggregation>` guide.
129+
:ref:`Aggregation <java-aggregation>` guide and the :ref:`Aggregation Expression
130+
Operations <java-aggregation-expression-operations>` page.
105131

106132
Example
107133
~~~~~~~
@@ -136,8 +162,21 @@ purchased color.
136162
For more information about how to construct an aggregation pipeline, see
137163
the {+mdb-server+} manual page on :manual:`Aggregation </aggregation>`.
138164

139-
For additional information on the methods mentioned on this page, see
140-
the following API Documentation:
165+
API Documentation
166+
-----------------
167+
168+
For additional information on the methods and classes used to retrieve documents
169+
on this page, see the following API Documentation:
170+
171+
- `find() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#find()>`__
172+
- `first() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoIterable.html#first()>`__
173+
- `FindIterable <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/FindIterable.html>`__
174+
- `aggregate() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#aggregate(java.util.List)>`__
175+
176+
Server Manual Entries
177+
---------------------
141178

142-
- `MongoCollection.find() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#find()>`__
143-
- `MongoCollection.aggregate() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#aggregate(java.util.List)>`__
179+
- :manual:`Collections </core/databases-and-collections/#collections>`
180+
- :manual:`Query Documents </tutorial/query-documents>`
181+
- :manual:`Aggregation </aggregation>`
182+
- :manual:`Aggregation stages </meta/aggregation-quick-reference/#stages>`

source/crud/write-operations/delete.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,11 @@ For additional information on the methods and classes used to delete documents,
184184
- `findOneAndDelete() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#findOneAndDelete(org.bson.conversions.Bson)>`__
185185
- `DeleteOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/DeleteOptions.html>`__
186186
- `FindOneAndDeleteOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/FindOneAndDeleteOptions.html>`__
187+
- `DeleteResult <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/result/DeleteResult.html>`__
187188

188189
Server Manual Entries
189190
---------------------
190191

191-
- :manual:`db.collection.deleteOne() </reference/method/db.collection.deleteMany/>`
192-
- :manual:`db.collection.deleteMany() </reference/method/db.collection.deleteOne/>`
192+
- :manual:`db.collection.deleteOne() </reference/method/db.collection.deleteOne/>`
193+
- :manual:`db.collection.deleteMany() </reference/method/db.collection.deleteMany/>`
193194
- :manual:`db.collection.findOneAndDelete() </reference/method/db.collection.findOneAndDelete/>`

source/crud/write-operations/modify.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ operation and an update many operation.
173173

174174
.. io-code-block::
175175

176-
.. input:: /includes/crud/Insert.java
176+
.. input:: /includes/crud/Update.java
177177
:language: java
178178
:dedent:
179179

@@ -283,14 +283,13 @@ documents match.
283283
ReplaceOne Example: Full File
284284
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
285285

286-
The following code is a complete, standalone file that performs an update one
287-
operation and an update many operation.
286+
The following code is a complete, standalone file that performs a replace one operation.
288287

289288
.. include:: /includes/crud/example-intro.rst
290289

291290
.. io-code-block::
292291

293-
.. input:: /includes/crud/Replace.java
292+
.. input:: /includes/crud/ReplaceOne.java
294293
:language: java
295294
:dedent:
296295

@@ -313,7 +312,7 @@ For additional information on the methods and classes used to modify documents,
313312
- `UpdateOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateOptions.html>`__
314313
- `replaceOne() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#replaceOne(org.bson.conversions.Bson,TDocument)>`__
315314
- `ReplaceOptions <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/ReplaceOptions.html?is-external=true>`__
316-
- - `UpdateResult <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/result/UpdateResult.html>`__
315+
- `UpdateResult <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/result/UpdateResult.html>`__
317316

318317
For additional information on the methods used in the examples on this
319318
page, see the following API Documentation:

source/includes/usage-examples/code-snippets/Find.java renamed to source/includes/crud/Find.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
// Retrieves documents that match a query filter by using the Java driver
2-
3-
package usage.examples;
4-
52
import static com.mongodb.client.model.Filters.lt;
63

74
import org.bson.Document;
@@ -15,6 +12,8 @@
1512
import com.mongodb.client.model.Projections;
1613
import com.mongodb.client.model.Sorts;
1714

15+
import static com.mongodb.client.model.Filters.eq;
16+
1817
public class Find {
1918
public static void main( String[] args ) {
2019

@@ -36,12 +35,20 @@ public static void main( String[] args ) {
3635
.sort(Sorts.descending("title")).iterator();
3736

3837
// Prints the results of the find operation as JSON
39-
try {
40-
while(cursor.hasNext()) {
41-
System.out.println(cursor.next().toJson());
42-
}
43-
} finally {
44-
cursor.close();
38+
System.out.println("Number of documents found with find(): " + cursor.available() + "\n");
39+
cursor.close();
40+
41+
// Retrieves the first matching document, applying a projection and a descending sort to the results
42+
Document doc = collection.find(eq("title", "The Room"))
43+
.projection(projectionFields)
44+
.sort(Sorts.descending("imdb.rating"))
45+
.first();
46+
47+
// Prints a message if there are no result documents, or prints the result document as JSON
48+
if (doc == null) {
49+
System.out.println("No results found.");
50+
} else {
51+
System.out.println("Document found with find().first()" + doc.toJson());
4552
}
4653
}
4754
}

0 commit comments

Comments
 (0)